Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 1 | /* Copyright 2015 Pierre Ossman <ossman@cendio.se> for Cendio AB |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 2 | * Copyright (C) 2015 D. R. Commander. All Rights Reserved. |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 3 | * |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 8 | * |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 13 | * |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * This program reads files produced by TightVNC's/TurboVNC's |
| 22 | * fbs-dump, which in turn takes files from rfbproxy. It is |
| 23 | * basically a dump of the RFB protocol from the server side after |
| 24 | * the ServerInit message. Mostly this consists of FramebufferUpdate |
| 25 | * message using the HexTile encoding. Screen size and pixel format |
| 26 | * are not encoded in the file and must be specified by the user. |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <math.h> |
| 32 | |
| 33 | #include <rdr/Exception.h> |
| 34 | #include <rdr/OutStream.h> |
| 35 | #include <rdr/FileInStream.h> |
| 36 | |
| 37 | #include <rfb/PixelFormat.h> |
| 38 | |
| 39 | #include <rfb/CConnection.h> |
| 40 | #include <rfb/CMsgReader.h> |
| 41 | #include <rfb/Decoder.h> |
| 42 | #include <rfb/UpdateTracker.h> |
| 43 | |
| 44 | #include <rfb/EncodeManager.h> |
| 45 | #include <rfb/SConnection.h> |
| 46 | #include <rfb/SMsgWriter.h> |
| 47 | |
| 48 | #include "util.h" |
| 49 | |
| 50 | static rfb::IntParameter width("width", "Frame buffer width", 0); |
| 51 | static rfb::IntParameter height("height", "Frame buffer height", 0); |
| 52 | |
| 53 | static rfb::StringParameter format("format", "Pixel format (e.g. bgr888)", ""); |
| 54 | |
| 55 | // The frame buffer (and output) is always this format |
| 56 | static const rfb::PixelFormat fbPF(32, 24, false, true, 255, 255, 255, 0, 8, 16); |
| 57 | |
| 58 | // Encodings to use |
| 59 | static const rdr::S32 encodings[] = { |
| 60 | rfb::encodingTight, rfb::encodingCopyRect, rfb::encodingRRE, |
| 61 | rfb::encodingHextile, rfb::encodingZRLE, rfb::pseudoEncodingLastRect, |
| 62 | rfb::pseudoEncodingQualityLevel0 + 8 }; |
| 63 | |
| 64 | class DummyOutStream : public rdr::OutStream { |
| 65 | public: |
| 66 | DummyOutStream(); |
| 67 | |
| 68 | virtual int length(); |
| 69 | virtual void flush(); |
| 70 | |
| 71 | private: |
| 72 | virtual int overrun(int itemSize, int nItems); |
| 73 | |
| 74 | int offset; |
| 75 | rdr::U8 buf[131072]; |
| 76 | }; |
| 77 | |
| 78 | class CConn : public rfb::CConnection { |
| 79 | public: |
| 80 | CConn(const char *filename); |
| 81 | ~CConn(); |
| 82 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 83 | void getStats(double& ratio, unsigned long long& bytes, |
| 84 | unsigned long long& rawEquivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 85 | |
| 86 | virtual void setDesktopSize(int w, int h); |
| 87 | virtual void setCursor(int, int, const rfb::Point&, void*, void*); |
| 88 | virtual void framebufferUpdateStart(); |
| 89 | virtual void framebufferUpdateEnd(); |
| 90 | virtual void dataRect(const rfb::Rect&, int); |
| 91 | virtual void setColourMapEntries(int, int, rdr::U16*); |
| 92 | virtual void bell(); |
| 93 | virtual void serverCutText(const char*, rdr::U32); |
| 94 | |
| 95 | public: |
| 96 | double decodeTime; |
| 97 | double encodeTime; |
| 98 | |
| 99 | protected: |
| 100 | rdr::FileInStream *in; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 101 | rfb::Decoder *decoders[rfb::encodingMax + 1]; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 102 | rfb::ManagedPixelBuffer pb; |
| 103 | rfb::SimpleUpdateTracker updates; |
| 104 | class SConn *sc; |
| 105 | }; |
| 106 | |
| 107 | class Manager : public rfb::EncodeManager { |
| 108 | public: |
| 109 | Manager(class rfb::SConnection *conn); |
| 110 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 111 | void getStats(double&, unsigned long long&, unsigned long long&); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | class SConn : public rfb::SConnection { |
| 115 | public: |
| 116 | SConn(); |
| 117 | ~SConn(); |
| 118 | |
| 119 | void writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb); |
| 120 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 121 | void getStats(double&, unsigned long long&, unsigned long long&); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 122 | |
| 123 | virtual void setAccessRights(AccessRights ar); |
| 124 | |
| 125 | virtual void setDesktopSize(int fb_width, int fb_height, |
| 126 | const rfb::ScreenSet& layout); |
| 127 | |
| 128 | protected: |
| 129 | DummyOutStream *out; |
| 130 | Manager *manager; |
| 131 | }; |
| 132 | |
| 133 | DummyOutStream::DummyOutStream() |
| 134 | { |
| 135 | offset = 0; |
| 136 | ptr = buf; |
| 137 | end = buf + sizeof(buf); |
| 138 | } |
| 139 | |
| 140 | int DummyOutStream::length() |
| 141 | { |
| 142 | flush(); |
| 143 | return offset; |
| 144 | } |
| 145 | |
| 146 | void DummyOutStream::flush() |
| 147 | { |
| 148 | offset += ptr - buf; |
| 149 | ptr = buf; |
| 150 | } |
| 151 | |
| 152 | int DummyOutStream::overrun(int itemSize, int nItems) |
| 153 | { |
| 154 | flush(); |
| 155 | } |
| 156 | |
| 157 | CConn::CConn(const char *filename) |
| 158 | { |
| 159 | int i; |
| 160 | |
| 161 | decodeTime = 0.0; |
| 162 | encodeTime = 0.0; |
| 163 | |
| 164 | in = new rdr::FileInStream(filename); |
| 165 | setStreams(in, NULL); |
| 166 | |
| 167 | memset(decoders, 0, sizeof(decoders)); |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 168 | for (i = 0; i < rfb::encodingMax; i++) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 169 | if (!rfb::Decoder::supported(i)) |
| 170 | continue; |
| 171 | |
| 172 | decoders[i] = rfb::Decoder::createDecoder(i, this); |
| 173 | } |
| 174 | |
| 175 | pb.setPF(fbPF); |
| 176 | |
| 177 | // Need to skip the initial handshake and ServerInit |
| 178 | setState(RFBSTATE_NORMAL); |
| 179 | // That also means that the reader and writer weren't setup |
| 180 | setReader(new rfb::CMsgReader(this, in)); |
| 181 | // Nor the frame buffer size and format |
| 182 | setDesktopSize(width, height); |
| 183 | rfb::PixelFormat pf; |
| 184 | pf.parse(format); |
| 185 | setPixelFormat(pf); |
| 186 | |
| 187 | sc = new SConn(); |
| 188 | sc->cp.setPF(pb.getPF()); |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 189 | sc->setEncodings(sizeof(encodings) / sizeof(*encodings), encodings); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | CConn::~CConn() |
| 193 | { |
| 194 | int i; |
| 195 | |
| 196 | delete sc; |
| 197 | |
| 198 | delete in; |
| 199 | |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 200 | for (i = 0; i < rfb::encodingMax; i++) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 201 | delete decoders[i]; |
| 202 | } |
| 203 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 204 | void CConn::getStats(double& ratio, unsigned long long& bytes, |
| 205 | unsigned long long& rawEquivalent) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 206 | { |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 207 | sc->getStats(ratio, bytes, rawEquivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void CConn::setDesktopSize(int w, int h) |
| 211 | { |
| 212 | CConnection::setDesktopSize(w, h); |
| 213 | |
| 214 | pb.setSize(cp.width, cp.height); |
| 215 | } |
| 216 | |
| 217 | void CConn::setCursor(int, int, const rfb::Point&, void*, void*) |
| 218 | { |
| 219 | } |
| 220 | |
| 221 | void CConn::framebufferUpdateStart() |
| 222 | { |
| 223 | updates.clear(); |
| 224 | } |
| 225 | |
| 226 | void CConn::framebufferUpdateEnd() |
| 227 | { |
| 228 | rfb::UpdateInfo ui; |
| 229 | rfb::Region clip(pb.getRect()); |
| 230 | |
| 231 | updates.getUpdateInfo(&ui, clip); |
| 232 | |
| 233 | startCpuCounter(); |
| 234 | sc->writeUpdate(ui, &pb); |
| 235 | endCpuCounter(); |
| 236 | |
| 237 | encodeTime += getCpuCounter(); |
| 238 | } |
| 239 | |
| 240 | void CConn::dataRect(const rfb::Rect &r, int encoding) |
| 241 | { |
| 242 | if (!decoders[encoding]) |
| 243 | throw rdr::Exception("Unknown encoding"); |
| 244 | |
| 245 | startCpuCounter(); |
| 246 | decoders[encoding]->readRect(r, &pb); |
| 247 | endCpuCounter(); |
| 248 | |
| 249 | decodeTime += getCpuCounter(); |
| 250 | |
| 251 | if (encoding != rfb::encodingCopyRect) // FIXME |
| 252 | updates.add_changed(rfb::Region(r)); |
| 253 | } |
| 254 | |
| 255 | void CConn::setColourMapEntries(int, int, rdr::U16*) |
| 256 | { |
| 257 | } |
| 258 | |
| 259 | void CConn::bell() |
| 260 | { |
| 261 | } |
| 262 | |
| 263 | void CConn::serverCutText(const char*, rdr::U32) |
| 264 | { |
| 265 | } |
| 266 | |
| 267 | Manager::Manager(class rfb::SConnection *conn) : |
| 268 | EncodeManager(conn) |
| 269 | { |
| 270 | } |
| 271 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 272 | void Manager::getStats(double& ratio, unsigned long long& encodedBytes, |
| 273 | unsigned long long& rawEquivalent) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 274 | { |
| 275 | StatsVector::iterator iter; |
| 276 | unsigned long long bytes, equivalent; |
| 277 | |
| 278 | bytes = equivalent = 0; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 279 | for (iter = stats.begin(); iter != stats.end(); ++iter) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 280 | StatsVector::value_type::iterator iter2; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 281 | for (iter2 = iter->begin(); iter2 != iter->end(); ++iter2) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 282 | bytes += iter2->bytes; |
| 283 | equivalent += iter2->equivalent; |
| 284 | } |
| 285 | } |
| 286 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 287 | ratio = (double)equivalent / bytes; |
| 288 | encodedBytes = bytes; |
| 289 | rawEquivalent = equivalent; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | SConn::SConn() |
| 293 | { |
| 294 | out = new DummyOutStream; |
| 295 | setStreams(NULL, out); |
| 296 | |
| 297 | setWriter(new rfb::SMsgWriter(&cp, out)); |
| 298 | |
| 299 | manager = new Manager(this); |
| 300 | } |
| 301 | |
| 302 | SConn::~SConn() |
| 303 | { |
| 304 | delete manager; |
| 305 | delete out; |
| 306 | } |
| 307 | |
| 308 | void SConn::writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb) |
| 309 | { |
| 310 | manager->writeUpdate(ui, pb, NULL); |
| 311 | } |
| 312 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 313 | void SConn::getStats(double& ratio, unsigned long long& bytes, |
| 314 | unsigned long long& rawEquivalent) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 315 | { |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 316 | manager->getStats(ratio, bytes, rawEquivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void SConn::setAccessRights(AccessRights ar) |
| 320 | { |
| 321 | } |
| 322 | |
| 323 | void SConn::setDesktopSize(int fb_width, int fb_height, |
| 324 | const rfb::ScreenSet& layout) |
| 325 | { |
| 326 | } |
| 327 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 328 | static double runTest(const char *fn, double& ratio, unsigned long long& bytes, |
| 329 | unsigned long long& rawEquivalent) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 330 | { |
| 331 | CConn *cc; |
| 332 | double time; |
| 333 | |
| 334 | cc = new CConn(fn); |
| 335 | |
| 336 | try { |
| 337 | while (true) |
| 338 | cc->processMsg(); |
| 339 | } catch (rdr::EndOfStream e) { |
| 340 | } catch (rdr::Exception e) { |
| 341 | fprintf(stderr, "Failed to run rfb file: %s\n", e.str()); |
| 342 | exit(1); |
| 343 | } |
| 344 | |
| 345 | time = cc->encodeTime; |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 346 | cc->getStats(ratio, bytes, rawEquivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 347 | |
| 348 | delete cc; |
| 349 | |
| 350 | return time; |
| 351 | } |
| 352 | |
| 353 | static void sort(double *array, int count) |
| 354 | { |
| 355 | bool sorted; |
| 356 | int i; |
| 357 | do { |
| 358 | sorted = true; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 359 | for (i = 1; i < count; i++) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 360 | if (array[i-1] > array[i]) { |
| 361 | double d; |
| 362 | d = array[i]; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 363 | array[i] = array[i - 1]; |
| 364 | array[i - 1] = d; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 365 | sorted = false; |
| 366 | } |
| 367 | } |
| 368 | } while (!sorted); |
| 369 | } |
| 370 | |
| 371 | static const int runCount = 9; |
| 372 | |
| 373 | static void usage(const char *argv0) |
| 374 | { |
| 375 | fprintf(stderr, "Syntax: %s [options] <rfb file>\n", argv0); |
| 376 | fprintf(stderr, "Options:\n"); |
| 377 | rfb::Configuration::listParams(79, 14); |
| 378 | exit(1); |
| 379 | } |
| 380 | |
| 381 | int main(int argc, char **argv) |
| 382 | { |
| 383 | int i; |
| 384 | |
| 385 | const char *fn; |
| 386 | |
| 387 | double times[runCount], dev[runCount]; |
| 388 | double median, meddev, ratio; |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 389 | unsigned long long bytes, equivalent; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 390 | |
| 391 | fn = NULL; |
| 392 | for (i = 1; i < argc; i++) { |
| 393 | if (rfb::Configuration::setParam(argv[i])) |
| 394 | continue; |
| 395 | |
| 396 | if (argv[i][0] == '-') { |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 397 | if (i + 1 < argc) { |
| 398 | if (rfb::Configuration::setParam(&argv[i][1], argv[i + 1])) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 399 | i++; |
| 400 | continue; |
| 401 | } |
| 402 | } |
| 403 | usage(argv[0]); |
| 404 | } |
| 405 | |
| 406 | if (fn != NULL) |
| 407 | usage(argv[0]); |
| 408 | |
| 409 | fn = argv[i]; |
| 410 | } |
| 411 | |
| 412 | if (fn == NULL) { |
| 413 | fprintf(stderr, "No file specified!\n\n"); |
| 414 | usage(argv[0]); |
| 415 | } |
| 416 | |
| 417 | if (!format.hasBeenSet()) { |
| 418 | fprintf(stderr, "Pixel format not specified!\n\n"); |
| 419 | usage(argv[0]); |
| 420 | } |
| 421 | |
| 422 | if (!width.hasBeenSet() || !height.hasBeenSet()) { |
| 423 | fprintf(stderr, "Frame buffer size not specified!\n\n"); |
| 424 | usage(argv[0]); |
| 425 | } |
| 426 | |
| 427 | // Warmup |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 428 | runTest(fn, ratio, bytes, equivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 429 | |
| 430 | // Multiple runs to get a good average |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 431 | for (i = 0; i < runCount; i++) |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 432 | times[i] = runTest(fn, ratio, bytes, equivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 433 | |
| 434 | // Calculate median and median deviation |
| 435 | sort(times, runCount); |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 436 | median = times[runCount / 2]; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 437 | |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 438 | for (i = 0; i < runCount; i++) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 439 | dev[i] = fabs((times[i] - median) / median) * 100; |
| 440 | |
| 441 | sort(dev, runCount); |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 442 | meddev = dev[runCount / 2]; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 443 | |
| 444 | printf("CPU time: %g s (+/- %g %)\n", median, meddev); |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame^] | 445 | printf("Encoded bytes: %lld\n", bytes); |
| 446 | printf("Raw equivalent bytes: %lld\n", equivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 447 | printf("Ratio: %g\n", ratio); |
| 448 | |
| 449 | return 0; |
| 450 | } |