Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 3 | #include <ast_video_types.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 4 | #include <boost/asio.hpp> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 5 | #include <cassert> |
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 | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 9 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 10 | namespace ast_video |
| 11 | { |
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 | // |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | struct AstCurAttributionTag |
| 17 | { |
| 18 | unsigned int posX; |
| 19 | unsigned int posY; |
| 20 | unsigned int curWidth; |
| 21 | unsigned int curHeight; |
| 22 | unsigned int curType; // 0:mono 1:color 2:disappear cursor |
| 23 | unsigned int curChangeFlag; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 24 | }; |
Ed Tanous | d5f3999 | 2017-04-18 13:41:22 -0700 | [diff] [blame] | 25 | |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 26 | // |
| 27 | // For storing Cursor Information |
| 28 | // |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | struct AstCursorTag |
| 30 | { |
| 31 | AstCurAttributionTag attr; |
| 32 | // unsigned char icon[MAX_CUR_OFFSETX*MAX_CUR_OFFSETY*2]; |
| 33 | unsigned char *icon; //[64*64*2]; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 34 | }; |
Ed Tanous | d5f3999 | 2017-04-18 13:41:22 -0700 | [diff] [blame] | 35 | |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 36 | // |
| 37 | // For select image format, i.e. 422 JPG420, 444 JPG444, lumin/chrom table, 0 |
| 38 | // ~ 11, low to high |
| 39 | // |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | struct FeaturesTag |
| 41 | { |
| 42 | short jpgFmt; // 422:JPG420, 444:JPG444 |
| 43 | short luminTbl; |
| 44 | short chromTbl; |
| 45 | short toleranceNoise; |
| 46 | int w; |
| 47 | int h; |
| 48 | unsigned char *buf; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 49 | }; |
Ed Tanous | d5f3999 | 2017-04-18 13:41:22 -0700 | [diff] [blame] | 50 | |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 51 | // |
| 52 | // For configure video engine control registers |
| 53 | // |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 54 | struct ImageInfo |
| 55 | { |
| 56 | short doImageRefresh; // Action 0:motion 1:fullframe 2:quick cursor |
| 57 | char qcValid; // quick cursor enable/disable |
| 58 | unsigned int len; |
| 59 | int crypttype; |
| 60 | char cryptkey[16]; |
| 61 | union |
| 62 | { |
| 63 | FeaturesTag features; |
| 64 | AstCursorTag cursorInfo; |
| 65 | } parameter; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 66 | }; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 67 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | class SimpleVideoPuller |
| 69 | { |
| 70 | public: |
| 71 | SimpleVideoPuller() : imageInfo(){}; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 72 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | void initialize() |
| 74 | { |
| 75 | std::cout << "Opening /dev/video\n"; |
| 76 | videoFd = open("/dev/video", O_RDWR); |
| 77 | if (videoFd == 0) |
| 78 | { |
| 79 | std::cout << "Failed to open /dev/video\n"; |
| 80 | throw std::runtime_error("Failed to open /dev/video"); |
| 81 | } |
| 82 | std::cout << "Opened successfully\n"; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 83 | } |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 84 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 85 | RawVideoBuffer readVideo() |
| 86 | { |
| 87 | assert(videoFd != 0); |
| 88 | RawVideoBuffer raw; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 89 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 90 | imageInfo.doImageRefresh = 1; // full frame refresh |
| 91 | imageInfo.qcValid = 0; // quick cursor disabled |
| 92 | imageInfo.parameter.features.buf = |
| 93 | reinterpret_cast<unsigned char *>(raw.buffer.data()); |
| 94 | imageInfo.crypttype = -1; |
| 95 | std::cout << "Writing\n"; |
| 96 | |
| 97 | int status; |
| 98 | /* |
| 99 | status = write(videoFd, reinterpret_cast<char*>(&imageInfo), |
| 100 | sizeof(imageInfo)); |
| 101 | if (status != sizeof(imageInfo)) { |
| 102 | std::cout << "Write failed. Return: " << status << "\n"; |
| 103 | perror("perror output:"); |
| 104 | } |
| 105 | |
| 106 | std::cout << "Write done\n"; |
| 107 | */ |
| 108 | std::cout << "Reading\n"; |
| 109 | status = read(videoFd, reinterpret_cast<char *>(&imageInfo), |
| 110 | sizeof(imageInfo)); |
| 111 | std::cout << "Done reading\n"; |
| 112 | |
| 113 | if (status != 0) |
| 114 | { |
| 115 | std::cerr << "Read failed with status " << status << "\n"; |
| 116 | } |
| 117 | |
| 118 | raw.buffer.resize(imageInfo.len); |
| 119 | |
| 120 | raw.height = imageInfo.parameter.features.h; |
| 121 | raw.width = imageInfo.parameter.features.w; |
| 122 | if (imageInfo.parameter.features.jpgFmt == 422) |
| 123 | { |
| 124 | raw.mode = YuvMode::YUV420; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | raw.mode = YuvMode::YUV444; |
| 129 | } |
| 130 | return raw; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 133 | private: |
| 134 | int videoFd{}; |
| 135 | ImageInfo imageInfo; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 136 | }; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 137 | |
| 138 | #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 139 | class AsyncVideoPuller |
| 140 | { |
| 141 | public: |
| 142 | using video_callback = std::function<void(RawVideoBuffer &)>; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 143 | |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 144 | explicit AsyncVideoPuller(boost::asio::io_context &ioService) : |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | imageInfo(), devVideo(ioService, open("/dev/video", O_RDWR)) |
| 146 | { |
| 147 | videobuf = std::make_shared<RawVideoBuffer>(); |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 148 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | imageInfo.doImageRefresh = 1; // full frame refresh |
| 150 | imageInfo.qcValid = 0; // quick cursor disabled |
| 151 | imageInfo.parameter.features.buf = |
| 152 | reinterpret_cast<unsigned char *>(videobuf->buffer.data()); |
| 153 | imageInfo.crypttype = -1; |
| 154 | }; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 155 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 156 | void registerCallback(video_callback &callback) |
| 157 | { |
| 158 | std::lock_guard<std::mutex> lock(callbackMutex); |
| 159 | callbacks.push_back(callback); |
| 160 | startRead(); |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 161 | } |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 162 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 163 | void startRead() |
| 164 | { |
| 165 | auto mutableBuffer = boost::asio::buffer(&imageInfo, sizeof(imageInfo)); |
| 166 | boost::asio::async_read(devVideo, mutableBuffer, |
| 167 | [this](const boost::system::error_code &ec, |
| 168 | std::size_t bytes_transferred) { |
| 169 | if (ec) |
| 170 | { |
| 171 | std::cerr << "Read failed with status " |
| 172 | << ec << "\n"; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | this->readDone(); |
| 177 | } |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | void readDone() |
| 182 | { |
| 183 | std::cout << "Done reading\n"; |
| 184 | videobuf->buffer.resize(imageInfo.len); |
| 185 | |
| 186 | videobuf->height = imageInfo.parameter.features.h; |
| 187 | videobuf->width = imageInfo.parameter.features.w; |
| 188 | if (imageInfo.parameter.features.jpgFmt == 422) |
| 189 | { |
| 190 | videobuf->mode = YuvMode::YUV420; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | videobuf->mode = YuvMode::YUV444; |
| 195 | } |
| 196 | std::lock_guard<std::mutex> lock(callbackMutex); |
| 197 | for (auto &callback : callbacks) |
| 198 | { |
| 199 | // TODO(ed) call callbacks async and double buffer frames |
| 200 | callback(*videobuf); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private: |
| 205 | std::shared_ptr<RawVideoBuffer> videobuf; |
| 206 | boost::asio::posix::stream_descriptor devVideo; |
| 207 | ImageInfo imageInfo; |
| 208 | std::mutex callbackMutex; |
| 209 | std::vector<video_callback> callbacks; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 210 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 211 | #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) |
| 212 | } // namespace ast_video |