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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 4 | #include <cassert> |
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 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 10 | namespace ast_video { |
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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 15 | struct AstCurAttributionTag { |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 16 | unsigned int posX; |
| 17 | unsigned int posY; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 18 | unsigned int curWidth; |
| 19 | unsigned int curHeight; |
| 20 | unsigned int curType; // 0:mono 1:color 2:disappear cursor |
| 21 | unsigned int curChangeFlag; |
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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 27 | struct AstCursorTag { |
| 28 | AstCurAttributionTag attr; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 37 | struct FeaturesTag { |
| 38 | short jpgFmt; // 422:JPG420, 444:JPG444 |
| 39 | short luminTbl; |
| 40 | short chromTbl; |
| 41 | short toleranceNoise; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 50 | struct ImageInfo { |
| 51 | short doImageRefresh; // Action 0:motion 1:fullframe 2:quick cursor |
| 52 | char qcValid; // quick cursor enable/disable |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 53 | unsigned int len; |
| 54 | int crypttype; |
| 55 | char cryptkey[16]; |
| 56 | union { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 57 | FeaturesTag features; |
| 58 | AstCursorTag cursorInfo; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 64 | SimpleVideoPuller() : imageInfo(){}; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 65 | |
| 66 | void initialize() { |
| 67 | std::cout << "Opening /dev/video\n"; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 68 | videoFd = open("/dev/video", O_RDWR); |
| 69 | if (videoFd == 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 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 76 | RawVideoBuffer readVideo() { |
| 77 | assert(videoFd != 0); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 78 | RawVideoBuffer raw; |
| 79 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 80 | imageInfo.doImageRefresh = 1; // full frame refresh |
| 81 | imageInfo.qcValid = 0; // quick cursor disabled |
| 82 | imageInfo.parameter.features.buf = |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 83 | reinterpret_cast<unsigned char *>(raw.buffer.data()); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 84 | imageInfo.crypttype = -1; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 85 | std::cout << "Writing\n"; |
| 86 | |
| 87 | int status; |
| 88 | /* |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 89 | status = write(videoFd, reinterpret_cast<char*>(&imageInfo), |
| 90 | sizeof(imageInfo)); |
| 91 | if (status != sizeof(imageInfo)) { |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 99 | status = |
| 100 | read(videoFd, reinterpret_cast<char *>(&imageInfo), sizeof(imageInfo)); |
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 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 107 | raw.buffer.resize(imageInfo.len); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 108 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 109 | raw.height = imageInfo.parameter.features.h; |
| 110 | raw.width = imageInfo.parameter.features.w; |
| 111 | if (imageInfo.parameter.features.jpgFmt == 422) { |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 112 | raw.mode = YuvMode::YUV420; |
| 113 | } else { |
| 114 | raw.mode = YuvMode::YUV444; |
| 115 | } |
| 116 | return raw; |
| 117 | } |
| 118 | |
| 119 | private: |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 120 | int videoFd{}; |
| 121 | ImageInfo imageInfo; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 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 | 55c7b7a | 2018-05-22 15:27:24 -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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 129 | explicit AsyncVideoPuller(boost::asio::io_service &ioService) |
| 130 | : imageInfo(), devVideo(ioService, open("/dev/video", O_RDWR)) { |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 131 | videobuf = std::make_shared<RawVideoBuffer>(); |
| 132 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 133 | imageInfo.doImageRefresh = 1; // full frame refresh |
| 134 | imageInfo.qcValid = 0; // quick cursor disabled |
| 135 | imageInfo.parameter.features.buf = |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 136 | reinterpret_cast<unsigned char *>(videobuf->buffer.data()); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 137 | imageInfo.crypttype = -1; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 140 | void registerCallback(video_callback &callback) { |
| 141 | std::lock_guard<std::mutex> lock(callbackMutex); |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 142 | callbacks.push_back(callback); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 143 | startRead(); |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 146 | void startRead() { |
| 147 | auto mutableBuffer = boost::asio::buffer(&imageInfo, sizeof(imageInfo)); |
| 148 | boost::asio::async_read(devVideo, mutableBuffer, |
| 149 | [this](const boost::system::error_code &ec, |
| 150 | std::size_t bytes_transferred) { |
| 151 | if (ec) { |
| 152 | std::cerr << "Read failed with status " << ec |
| 153 | << "\n"; |
| 154 | } else { |
| 155 | this->readDone(); |
| 156 | } |
| 157 | }); |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 160 | void readDone() { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 161 | std::cout << "Done reading\n"; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 162 | videobuf->buffer.resize(imageInfo.len); |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 163 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 164 | videobuf->height = imageInfo.parameter.features.h; |
| 165 | videobuf->width = imageInfo.parameter.features.w; |
| 166 | if (imageInfo.parameter.features.jpgFmt == 422) { |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 167 | videobuf->mode = YuvMode::YUV420; |
| 168 | } else { |
| 169 | videobuf->mode = YuvMode::YUV444; |
| 170 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 171 | std::lock_guard<std::mutex> lock(callbackMutex); |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 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; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 180 | boost::asio::posix::stream_descriptor devVideo; |
| 181 | ImageInfo imageInfo; |
| 182 | std::mutex callbackMutex; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 183 | std::vector<video_callback> callbacks; |
| 184 | }; |
| 185 | #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 186 | } // namespace ast_video |