Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 1 | #include "ikvm_server.hpp" |
| 2 | |
Charles Kearney | 8e909b7 | 2023-03-29 21:11:23 +0000 | [diff] [blame] | 3 | #include <linux/videodev2.h> |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 4 | #include <rfb/rfbproto.h> |
| 5 | |
George Liu | f79f6f5 | 2022-07-06 09:32:35 +0800 | [diff] [blame] | 6 | #include <boost/crc.hpp> |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
| 8 | #include <phosphor-logging/elog.hpp> |
| 9 | #include <phosphor-logging/log.hpp> |
| 10 | #include <xyz/openbmc_project/Common/error.hpp> |
| 11 | |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 12 | namespace ikvm |
| 13 | { |
| 14 | |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 15 | using namespace phosphor::logging; |
| 16 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 17 | |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 18 | Server::Server(const Args& args, Input& i, Video& v) : |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 19 | pendingResize(false), frameCounter(0), numClients(0), input(i), video(v) |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 20 | { |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 21 | std::string ip("localhost"); |
| 22 | const Args::CommandLine& commandLine = args.getCommandLine(); |
| 23 | int argc = commandLine.argc; |
| 24 | |
| 25 | server = rfbGetScreen(&argc, commandLine.argv, video.getWidth(), |
| 26 | video.getHeight(), Video::bitsPerSample, |
| 27 | Video::samplesPerPixel, Video::bytesPerPixel); |
| 28 | |
| 29 | if (!server) |
| 30 | { |
| 31 | log<level::ERR>("Failed to get VNC screen due to invalid arguments"); |
| 32 | elog<InvalidArgument>( |
| 33 | xyz::openbmc_project::Common::InvalidArgument::ARGUMENT_NAME(""), |
| 34 | xyz::openbmc_project::Common::InvalidArgument::ARGUMENT_VALUE("")); |
| 35 | } |
| 36 | |
| 37 | framebuffer.resize( |
| 38 | video.getHeight() * video.getWidth() * Video::bytesPerPixel, 0); |
| 39 | |
| 40 | server->screenData = this; |
| 41 | server->desktopName = "OpenBMC IKVM"; |
| 42 | server->frameBuffer = framebuffer.data(); |
| 43 | server->newClientHook = newClient; |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 44 | server->cursor = rfbMakeXCursor(cursorWidth, cursorHeight, (char*)cursor, |
| 45 | (char*)cursorMask); |
| 46 | server->cursor->xhot = 1; |
| 47 | server->cursor->yhot = 1; |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 48 | |
| 49 | rfbStringToAddr(&ip[0], &server->listenInterface); |
| 50 | |
| 51 | rfbInitServer(server); |
| 52 | |
| 53 | rfbMarkRectAsModified(server, 0, 0, video.getWidth(), video.getHeight()); |
| 54 | |
| 55 | server->kbdAddEvent = Input::keyEvent; |
| 56 | server->ptrAddEvent = Input::pointerEvent; |
| 57 | |
| 58 | processTime = (1000000 / video.getFrameRate()) - 100; |
Paul Fertser | 2d2f3da | 2021-06-18 11:16:43 +0000 | [diff] [blame] | 59 | |
| 60 | calcFrameCRC = args.getCalcFrameCRC(); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | Server::~Server() |
| 64 | { |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 65 | rfbScreenCleanup(server); |
| 66 | } |
| 67 | |
| 68 | void Server::resize() |
| 69 | { |
| 70 | if (frameCounter > video.getFrameRate()) |
| 71 | { |
| 72 | doResize(); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | pendingResize = true; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void Server::run() |
| 81 | { |
| 82 | rfbProcessEvents(server, processTime); |
| 83 | |
| 84 | if (server->clientHead) |
| 85 | { |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 86 | frameCounter++; |
| 87 | if (pendingResize && frameCounter > video.getFrameRate()) |
| 88 | { |
| 89 | doResize(); |
| 90 | pendingResize = false; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void Server::sendFrame() |
| 96 | { |
| 97 | char* data = video.getData(); |
| 98 | rfbClientIteratorPtr it; |
| 99 | rfbClientPtr cl; |
Paul Fertser | 2d2f3da | 2021-06-18 11:16:43 +0000 | [diff] [blame] | 100 | int64_t frame_crc = -1; |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 101 | |
| 102 | if (!data || pendingResize) |
| 103 | { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | it = rfbGetClientIterator(server); |
| 108 | |
| 109 | while ((cl = rfbClientIteratorNext(it))) |
| 110 | { |
| 111 | ClientData* cd = (ClientData*)cl->clientData; |
| 112 | rfbFramebufferUpdateMsg* fu = (rfbFramebufferUpdateMsg*)cl->updateBuf; |
| 113 | |
| 114 | if (!cd) |
| 115 | { |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | if (cd->skipFrame) |
| 120 | { |
| 121 | cd->skipFrame--; |
| 122 | continue; |
| 123 | } |
| 124 | |
Jae Hyun Yoo | 85d0455 | 2019-05-09 16:26:53 -0700 | [diff] [blame] | 125 | if (!cd->needUpdate) |
| 126 | { |
| 127 | continue; |
| 128 | } |
Paul Fertser | 2d2f3da | 2021-06-18 11:16:43 +0000 | [diff] [blame] | 129 | |
| 130 | if (calcFrameCRC) |
| 131 | { |
| 132 | if (frame_crc == -1) |
| 133 | { |
| 134 | /* JFIF header contains some varying data so skip it for |
| 135 | * checksum calculation */ |
Patrick Williams | 94f5f42 | 2024-08-16 15:22:08 -0400 | [diff] [blame^] | 136 | frame_crc = |
| 137 | boost::crc<32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, |
| 138 | true>(data + 0x30, video.getFrameSize() - 0x30); |
Paul Fertser | 2d2f3da | 2021-06-18 11:16:43 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | if (cd->last_crc == frame_crc) |
| 142 | { |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | cd->last_crc = frame_crc; |
| 147 | } |
| 148 | |
Jae Hyun Yoo | 85d0455 | 2019-05-09 16:26:53 -0700 | [diff] [blame] | 149 | cd->needUpdate = false; |
| 150 | |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 151 | if (cl->enableLastRectEncoding) |
| 152 | { |
| 153 | fu->nRects = 0xFFFF; |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | fu->nRects = Swap16IfLE(1); |
| 158 | } |
| 159 | |
Charles Kearney | 8e909b7 | 2023-03-29 21:11:23 +0000 | [diff] [blame] | 160 | switch (video.getPixelformat()) |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 161 | { |
Charles Kearney | 8e909b7 | 2023-03-29 21:11:23 +0000 | [diff] [blame] | 162 | case V4L2_PIX_FMT_RGB24: |
| 163 | framebuffer.assign(data, data + video.getFrameSize()); |
| 164 | rfbMarkRectAsModified(server, 0, 0, video.getWidth(), |
| 165 | video.getHeight()); |
| 166 | break; |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 167 | |
Charles Kearney | 8e909b7 | 2023-03-29 21:11:23 +0000 | [diff] [blame] | 168 | case V4L2_PIX_FMT_JPEG: |
| 169 | fu->type = rfbFramebufferUpdate; |
| 170 | cl->ublen = sz_rfbFramebufferUpdateMsg; |
| 171 | rfbSendUpdateBuf(cl); |
| 172 | cl->tightEncoding = rfbEncodingTight; |
| 173 | rfbSendTightHeader(cl, 0, 0, video.getWidth(), |
| 174 | video.getHeight()); |
| 175 | cl->updateBuf[cl->ublen++] = (char)(rfbTightJpeg << 4); |
| 176 | rfbSendCompressedDataTight(cl, data, video.getFrameSize()); |
| 177 | if (cl->enableLastRectEncoding) |
| 178 | { |
| 179 | rfbSendLastRectMarker(cl); |
| 180 | } |
| 181 | rfbSendUpdateBuf(cl); |
| 182 | break; |
| 183 | |
| 184 | default: |
| 185 | break; |
| 186 | } |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | rfbReleaseClientIterator(it); |
| 190 | } |
| 191 | |
Jae Hyun Yoo | 85d0455 | 2019-05-09 16:26:53 -0700 | [diff] [blame] | 192 | void Server::clientFramebufferUpdateRequest( |
George Liu | f79f6f5 | 2022-07-06 09:32:35 +0800 | [diff] [blame] | 193 | rfbClientPtr cl, rfbFramebufferUpdateRequestMsg* furMsg) |
Jae Hyun Yoo | 85d0455 | 2019-05-09 16:26:53 -0700 | [diff] [blame] | 194 | { |
George Liu | f79f6f5 | 2022-07-06 09:32:35 +0800 | [diff] [blame] | 195 | ClientData* cd = (ClientData*)cl->clientData; |
Jae Hyun Yoo | 85d0455 | 2019-05-09 16:26:53 -0700 | [diff] [blame] | 196 | |
| 197 | if (!cd) |
| 198 | return; |
| 199 | |
| 200 | // Ignore the furMsg info. This service uses full frame update always. |
| 201 | (void)furMsg; |
| 202 | |
| 203 | cd->needUpdate = true; |
| 204 | } |
| 205 | |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 206 | void Server::clientGone(rfbClientPtr cl) |
| 207 | { |
| 208 | Server* server = (Server*)cl->screen->screenData; |
| 209 | |
| 210 | delete (ClientData*)cl->clientData; |
Jae Hyun Yoo | 0049bfa | 2019-03-06 15:39:58 -0800 | [diff] [blame] | 211 | cl->clientData = nullptr; |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 212 | |
| 213 | if (server->numClients-- == 1) |
| 214 | { |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 215 | server->input.disconnect(); |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 216 | rfbMarkRectAsModified(server->server, 0, 0, server->video.getWidth(), |
| 217 | server->video.getHeight()); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | enum rfbNewClientAction Server::newClient(rfbClientPtr cl) |
| 222 | { |
| 223 | Server* server = (Server*)cl->screen->screenData; |
| 224 | |
Patrick Williams | 94f5f42 | 2024-08-16 15:22:08 -0400 | [diff] [blame^] | 225 | cl->clientData = |
| 226 | new ClientData(server->video.getFrameRate(), &server->input); |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 227 | cl->clientGoneHook = clientGone; |
Jae Hyun Yoo | 85d0455 | 2019-05-09 16:26:53 -0700 | [diff] [blame] | 228 | cl->clientFramebufferUpdateRequestHook = clientFramebufferUpdateRequest; |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 229 | if (!server->numClients++) |
| 230 | { |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 231 | server->input.connect(); |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 232 | server->pendingResize = false; |
| 233 | server->frameCounter = 0; |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return RFB_CLIENT_ACCEPT; |
| 237 | } |
| 238 | |
| 239 | void Server::doResize() |
| 240 | { |
| 241 | rfbClientIteratorPtr it; |
| 242 | rfbClientPtr cl; |
| 243 | |
| 244 | framebuffer.resize( |
| 245 | video.getHeight() * video.getWidth() * Video::bytesPerPixel, 0); |
| 246 | |
| 247 | rfbNewFramebuffer(server, framebuffer.data(), video.getWidth(), |
| 248 | video.getHeight(), Video::bitsPerSample, |
| 249 | Video::samplesPerPixel, Video::bytesPerPixel); |
| 250 | rfbMarkRectAsModified(server, 0, 0, video.getWidth(), video.getHeight()); |
| 251 | |
| 252 | it = rfbGetClientIterator(server); |
| 253 | |
| 254 | while ((cl = rfbClientIteratorNext(it))) |
| 255 | { |
| 256 | ClientData* cd = (ClientData*)cl->clientData; |
| 257 | |
| 258 | if (!cd) |
| 259 | { |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | // delay video updates to give the client time to resize |
| 264 | cd->skipFrame = video.getFrameRate(); |
| 265 | } |
| 266 | |
| 267 | rfbReleaseClientIterator(it); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | } // namespace ikvm |