Ed Tanous | eb1b862 | 2017-04-25 14:11:46 -0700 | [diff] [blame] | 1 | #include <string> |
Ed Tanous | 7d3dba4 | 2017-04-05 13:04:39 -0700 | [diff] [blame] | 2 | #include <crow/app.h> |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 3 | #include <boost/endian/arithmetic.hpp> |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 4 | |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 5 | #include <ast_jpeg_decoder.hpp> |
| 6 | #include <ast_video_puller.hpp> |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 7 | |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 8 | namespace crow { |
| 9 | namespace kvm { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 10 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 11 | static const std::string rfb33VersionString = "RFB 003.003\n"; |
| 12 | static const std::string rfb37VersionString = "RFB 003.007\n"; |
| 13 | static const std::string rfb38VersionString = "RFB 003.008\n"; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 14 | |
| 15 | enum class RfbAuthScheme : uint8_t { |
| 16 | connection_failed = 0, |
| 17 | no_authentication = 1, |
| 18 | vnc_authentication = 2 |
| 19 | }; |
| 20 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 21 | struct PixelFormatStruct { |
| 22 | boost::endian::big_uint8_t bitsPerPixel; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 23 | boost::endian::big_uint8_t depth; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 24 | boost::endian::big_uint8_t isBigEndian; |
| 25 | boost::endian::big_uint8_t isTrueColor; |
| 26 | boost::endian::big_uint16_t redMax; |
| 27 | boost::endian::big_uint16_t greenMax; |
| 28 | boost::endian::big_uint16_t blueMax; |
| 29 | boost::endian::big_uint8_t redShift; |
| 30 | boost::endian::big_uint8_t greenShift; |
| 31 | boost::endian::big_uint8_t blueShift; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 32 | boost::endian::big_uint8_t pad1; |
| 33 | boost::endian::big_uint8_t pad2; |
| 34 | boost::endian::big_uint8_t pad3; |
| 35 | }; |
| 36 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 37 | struct ServerInitializationMsg { |
| 38 | boost::endian::big_uint16_t framebufferWidth; |
| 39 | boost::endian::big_uint16_t framebufferHeight; |
| 40 | PixelFormatStruct pixelFormat; |
| 41 | boost::endian::big_uint32_t nameLength; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 44 | enum class client_to_server_msg_type : uint8_t { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 45 | set_pixel_format = 0, |
| 46 | fix_color_map_entries = 1, |
| 47 | set_encodings = 2, |
| 48 | framebuffer_update_request = 3, |
| 49 | key_event = 4, |
| 50 | pointer_event = 5, |
| 51 | client_cut_text = 6 |
| 52 | }; |
| 53 | |
Ed Tanous | eb1b862 | 2017-04-25 14:11:46 -0700 | [diff] [blame] | 54 | enum class server_to_client_message_type : uint8_t { |
| 55 | framebuffer_update = 0, |
| 56 | set_color_map_entries = 1, |
| 57 | bell_message = 2, |
| 58 | server_cut_text = 3 |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 61 | struct SetPixelFormatMsg { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 62 | boost::endian::big_uint8_t pad1; |
| 63 | boost::endian::big_uint8_t pad2; |
| 64 | boost::endian::big_uint8_t pad3; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 65 | PixelFormatStruct pixelFormat; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 68 | struct FrameBufferUpdateReq { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 69 | boost::endian::big_uint8_t incremental; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 70 | boost::endian::big_uint16_t xPosition; |
| 71 | boost::endian::big_uint16_t yPosition; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 72 | boost::endian::big_uint16_t width; |
| 73 | boost::endian::big_uint16_t height; |
| 74 | }; |
| 75 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 76 | struct KeyEventMsg { |
| 77 | boost::endian::big_uint8_t downFlag; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 78 | boost::endian::big_uint8_t pad1; |
| 79 | boost::endian::big_uint8_t pad2; |
| 80 | boost::endian::big_uint32_t key; |
| 81 | }; |
| 82 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 83 | struct PointerEventMsg { |
| 84 | boost::endian::big_uint8_t buttonMask; |
| 85 | boost::endian::big_uint16_t xPosition; |
| 86 | boost::endian::big_uint16_t yPosition; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 89 | struct ClientCutTextMsg { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 90 | std::vector<uint8_t> data; |
| 91 | }; |
| 92 | |
| 93 | enum class encoding_type : uint32_t { |
| 94 | raw = 0x00, |
| 95 | copy_rectangle = 0x01, |
| 96 | rising_rectangle = 0x02, |
| 97 | corre = 0x04, |
| 98 | hextile = 0x05, |
| 99 | zlib = 0x06, |
| 100 | tight = 0x07, |
| 101 | zlibhex = 0x08, |
| 102 | ultra = 0x09, |
| 103 | zrle = 0x10, |
| 104 | zywrle = 0x011, |
| 105 | cache_enable = 0xFFFF0001, |
| 106 | xor_enable = 0xFFFF0006, |
| 107 | server_state_ultranvc = 0xFFFF8000, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 108 | enable_keepAlive = 0xFFFF8001, |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 109 | enableftp_protocol_version = 0xFFFF8002, |
| 110 | tight_compress_level_0 = 0xFFFFFF00, |
| 111 | tight_compress_level_9 = 0xFFFFFF09, |
| 112 | x_cursor = 0xFFFFFF10, |
| 113 | rich_cursor = 0xFFFFFF11, |
| 114 | pointer_pos = 0xFFFFFF18, |
| 115 | last_rect = 0xFFFFFF20, |
| 116 | new_framebuffer_size = 0xFFFFFF21, |
| 117 | tight_quality_level_0 = 0xFFFFFFE0, |
| 118 | tight_quality_level_9 = 0xFFFFFFE9 |
| 119 | }; |
| 120 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 121 | struct FramebufferRectangle { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 122 | boost::endian::big_uint16_t x{}; |
| 123 | boost::endian::big_uint16_t y{}; |
| 124 | boost::endian::big_uint16_t width{}; |
| 125 | boost::endian::big_uint16_t height{}; |
| 126 | boost::endian::big_uint32_t encoding{}; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 127 | std::vector<uint8_t> data; |
| 128 | }; |
| 129 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 130 | struct FramebufferUpdateMsg { |
| 131 | boost::endian::big_uint8_t messageType{}; |
| 132 | std::vector<FramebufferRectangle> rectangles; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 135 | inline std::string serialize(const FramebufferUpdateMsg& msg) { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 136 | // calculate the size of the needed vector for serialization |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 137 | size_t vectorSize = 4; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 138 | for (const auto& rect : msg.rectangles) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 139 | vectorSize += 12 + rect.data.size(); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 142 | std::string serialized(vectorSize, 0); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 143 | |
| 144 | size_t i = 0; |
Ed Tanous | eb1b862 | 2017-04-25 14:11:46 -0700 | [diff] [blame] | 145 | serialized[i++] = static_cast<char>( |
| 146 | server_to_client_message_type::framebuffer_update); // Type |
| 147 | serialized[i++] = 0; // Pad byte |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 148 | boost::endian::big_uint16_t numberOfRectangles = msg.rectangles.size(); |
| 149 | std::memcpy(&serialized[i], &numberOfRectangles, sizeof(numberOfRectangles)); |
| 150 | i += sizeof(numberOfRectangles); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 151 | |
| 152 | for (const auto& rect : msg.rectangles) { |
| 153 | // copy the first part of the struct |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 154 | size_t bufferSize = |
| 155 | sizeof(FramebufferRectangle) - sizeof(std::vector<uint8_t>); |
| 156 | std::memcpy(&serialized[i], &rect, bufferSize); |
| 157 | i += bufferSize; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 158 | |
| 159 | std::memcpy(&serialized[i], rect.data.data(), rect.data.size()); |
| 160 | i += rect.data.size(); |
| 161 | } |
| 162 | |
| 163 | return serialized; |
| 164 | } |
| 165 | |
| 166 | enum class VncState { |
| 167 | UNSTARTED, |
| 168 | AWAITING_CLIENT_VERSION, |
| 169 | AWAITING_CLIENT_AUTH_METHOD, |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 170 | AWAITING_CLIENT_INIT_msg, |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 171 | MAIN_LOOP |
| 172 | }; |
| 173 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 174 | class ConnectionMetadata { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 175 | public: |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 176 | ConnectionMetadata(){}; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 177 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 178 | VncState vncState{VncState::UNSTARTED}; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 181 | using meta_list = std::vector<ConnectionMetadata>; |
| 182 | meta_list connectionStates(10); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 183 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 184 | ConnectionMetadata meta; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 185 | |
Ed Tanous | 7d3dba4 | 2017-04-05 13:04:39 -0700 | [diff] [blame] | 186 | template <typename... Middlewares> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 187 | void requestRoutes(Crow<Middlewares...>& app) { |
| 188 | BMCWEB_ROUTE(app, "/kvmws") |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 189 | .websocket() |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 190 | .onopen([&](crow::websocket::Connection& conn) { |
| 191 | if (meta.vncState == VncState::UNSTARTED) { |
| 192 | meta.vncState = VncState::AWAITING_CLIENT_VERSION; |
| 193 | conn.sendBinary(rfb38VersionString); |
Ed Tanous | 01250f2 | 2017-04-18 17:49:51 -0700 | [diff] [blame] | 194 | } else { // SHould never happen |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 195 | conn.close(); |
| 196 | } |
| 197 | |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 198 | }) |
| 199 | .onclose( |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 200 | [&](crow::websocket::Connection& conn, const std::string& reason) { |
| 201 | meta.vncState = VncState::UNSTARTED; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 202 | }) |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 203 | .onmessage([&](crow::websocket::Connection& conn, const std::string& data, |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 204 | bool is_binary) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 205 | switch (meta.vncState) { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 206 | case VncState::AWAITING_CLIENT_VERSION: { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 207 | std::cout << "Client sent: " << data; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 208 | if (data == rfb38VersionString || data == rfb37VersionString) { |
| 209 | std::string authTypes{1, |
| 210 | (uint8_t)RfbAuthScheme::no_authentication}; |
| 211 | conn.sendBinary(authTypes); |
| 212 | meta.vncState = VncState::AWAITING_CLIENT_AUTH_METHOD; |
| 213 | } else if (data == rfb33VersionString) { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 214 | // TODO(ed) Support older protocols |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 215 | meta.vncState = VncState::UNSTARTED; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 216 | conn.close(); |
| 217 | } else { |
| 218 | // TODO(ed) Support older protocols |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 219 | meta.vncState = VncState::UNSTARTED; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 220 | conn.close(); |
| 221 | } |
| 222 | } break; |
| 223 | case VncState::AWAITING_CLIENT_AUTH_METHOD: { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 224 | std::string securityResult{{0, 0, 0, 0}}; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 225 | if (data[0] == (uint8_t)RfbAuthScheme::no_authentication) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 226 | meta.vncState = VncState::AWAITING_CLIENT_INIT_msg; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 227 | } else { |
| 228 | // Mark auth as failed |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 229 | securityResult[3] = 1; |
| 230 | meta.vncState = VncState::UNSTARTED; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 231 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 232 | conn.sendBinary(securityResult); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 233 | } break; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 234 | case VncState::AWAITING_CLIENT_INIT_msg: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 235 | // Now send the server initialization |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 236 | ServerInitializationMsg serverInitMsg{}; |
| 237 | serverInitMsg.framebufferWidth = 800; |
| 238 | serverInitMsg.framebufferHeight = 600; |
| 239 | serverInitMsg.pixelFormat.bitsPerPixel = 32; |
| 240 | serverInitMsg.pixelFormat.isBigEndian = 0; |
| 241 | serverInitMsg.pixelFormat.isTrueColor = 1; |
| 242 | serverInitMsg.pixelFormat.redMax = 255; |
| 243 | serverInitMsg.pixelFormat.greenMax = 255; |
| 244 | serverInitMsg.pixelFormat.blueMax = 255; |
| 245 | serverInitMsg.pixelFormat.redShift = 16; |
| 246 | serverInitMsg.pixelFormat.greenShift = 8; |
| 247 | serverInitMsg.pixelFormat.blueShift = 0; |
| 248 | serverInitMsg.nameLength = 0; |
| 249 | std::cout << "size: " << sizeof(serverInitMsg); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 250 | // TODO(ed) this is ugly. Crow should really have a span type |
| 251 | // interface |
| 252 | // to avoid the copy, but alas, today it does not. |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 253 | std::string s(reinterpret_cast<char*>(&serverInitMsg), |
| 254 | sizeof(serverInitMsg)); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 255 | std::cout << "s.size() " << s.size(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 256 | conn.sendBinary(s); |
| 257 | meta.vncState = VncState::MAIN_LOOP; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 258 | } break; |
| 259 | case VncState::MAIN_LOOP: { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 260 | if (data.size() >= sizeof(client_to_server_msg_type)) { |
| 261 | auto type = static_cast<client_to_server_msg_type>(data[0]); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 262 | std::cout << "Received client message type " |
| 263 | << static_cast<std::size_t>(type) << "\n"; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 264 | switch (type) { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 265 | case client_to_server_msg_type::set_pixel_format: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 266 | } break; |
| 267 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 268 | case client_to_server_msg_type::fix_color_map_entries: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 269 | } break; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 270 | case client_to_server_msg_type::set_encodings: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 271 | } break; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 272 | case client_to_server_msg_type::framebuffer_update_request: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 273 | // Make sure the buffer is long enough to handle what we're |
| 274 | // about to do |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 275 | if (data.size() >= sizeof(FrameBufferUpdateReq) + |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 276 | sizeof(client_to_server_msg_type)) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 277 | auto msg = reinterpret_cast<const FrameBufferUpdateReq*>( |
| 278 | data.data() + // NOLINT |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 279 | sizeof(client_to_server_msg_type)); |
| 280 | // TODO(ed) find a better way to do this deserialization |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 281 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 282 | // Todo(ed) lifecycle of the video puller and decoder |
| 283 | // should be |
| 284 | // with the websocket, not recreated every time |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 285 | ast_video::SimpleVideoPuller p; |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 286 | p.initialize(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 287 | auto out = p.readVideo(); |
| 288 | ast_video::AstJpegDecoder d; |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 289 | d.decode(out.buffer, out.width, out.height, out.mode, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 290 | out.ySelector, out.uvSelector); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 291 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 292 | FramebufferUpdateMsg bufferUpdateMsg; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 293 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 294 | // If the viewer is requesting a full update, force write |
| 295 | // of all pixels |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 296 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 297 | FramebufferRectangle thisRect; |
| 298 | thisRect.x = msg->xPosition; |
| 299 | thisRect.y = msg->yPosition; |
| 300 | thisRect.width = out.width; |
| 301 | thisRect.height = out.height; |
| 302 | thisRect.encoding = |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 303 | static_cast<uint8_t>(encoding_type::raw); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 304 | std::cout << "Encoding is " << thisRect.encoding; |
| 305 | thisRect.data.reserve( |
| 306 | static_cast<std::size_t>(thisRect.width) * |
| 307 | static_cast<std::size_t>(thisRect.height) * 4); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 308 | std::cout << "Width " << out.width << " Height " |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 309 | << out.height; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 310 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 311 | for (int i = 0; i < out.width * out.height; i++) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 312 | auto& pixel = d.outBuffer[i]; |
| 313 | thisRect.data.push_back(pixel.b); |
| 314 | thisRect.data.push_back(pixel.g); |
| 315 | thisRect.data.push_back(pixel.r); |
| 316 | thisRect.data.push_back(0); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 317 | } |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 318 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 319 | bufferUpdateMsg.rectangles.push_back(std::move(thisRect)); |
| 320 | auto serialized = serialize(bufferUpdateMsg); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 321 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 322 | conn.sendBinary(serialized); |
Ed Tanous | 01250f2 | 2017-04-18 17:49:51 -0700 | [diff] [blame] | 323 | |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 324 | } // TODO(Ed) handle error |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 325 | |
| 326 | } |
| 327 | |
| 328 | break; |
| 329 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 330 | case client_to_server_msg_type::key_event: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 331 | } break; |
| 332 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 333 | case client_to_server_msg_type::pointer_event: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 334 | } break; |
| 335 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 336 | case client_to_server_msg_type::client_cut_text: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 337 | } break; |
| 338 | |
| 339 | default: |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | } break; |
| 345 | case VncState::UNSTARTED: |
| 346 | // Error? TODO |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | }); |
| 351 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 352 | } // namespace kvm |
| 353 | } // namespace crow |