blob: 4112a37e3c109d183aebba9f0e5817642062782c [file] [log] [blame]
Pierre Ossman8738e8a2015-02-11 13:49:04 +01001/* Copyright 2015 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC77be9292015-02-21 11:44:26 -06002 * Copyright (C) 2015 D. R. Commander. All Rights Reserved.
DRCb4c4a382015-02-21 11:28:37 -06003 *
Pierre Ossman8738e8a2015-02-11 13:49:04 +01004 * 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.
DRCb4c4a382015-02-21 11:28:37 -06008 *
Pierre Ossman8738e8a2015-02-11 13:49:04 +01009 * 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.
DRCb4c4a382015-02-21 11:28:37 -060013 *
Pierre Ossman8738e8a2015-02-11 13:49:04 +010014 * 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
50static rfb::IntParameter width("width", "Frame buffer width", 0);
51static rfb::IntParameter height("height", "Frame buffer height", 0);
52
53static rfb::StringParameter format("format", "Pixel format (e.g. bgr888)", "");
54
55// The frame buffer (and output) is always this format
56static const rfb::PixelFormat fbPF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
57
58// Encodings to use
59static const rdr::S32 encodings[] = {
60 rfb::encodingTight, rfb::encodingCopyRect, rfb::encodingRRE,
61 rfb::encodingHextile, rfb::encodingZRLE, rfb::pseudoEncodingLastRect,
62 rfb::pseudoEncodingQualityLevel0 + 8 };
63
64class DummyOutStream : public rdr::OutStream {
65public:
66 DummyOutStream();
67
68 virtual int length();
69 virtual void flush();
70
71private:
72 virtual int overrun(int itemSize, int nItems);
73
74 int offset;
75 rdr::U8 buf[131072];
76};
77
78class CConn : public rfb::CConnection {
79public:
80 CConn(const char *filename);
81 ~CConn();
82
DRC77be9292015-02-21 11:44:26 -060083 void getStats(double& ratio, unsigned long long& bytes,
84 unsigned long long& rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +010085
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
95public:
96 double decodeTime;
97 double encodeTime;
98
99protected:
100 rdr::FileInStream *in;
DRCb4c4a382015-02-21 11:28:37 -0600101 rfb::Decoder *decoders[rfb::encodingMax + 1];
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100102 rfb::ManagedPixelBuffer pb;
103 rfb::SimpleUpdateTracker updates;
104 class SConn *sc;
105};
106
107class Manager : public rfb::EncodeManager {
108public:
109 Manager(class rfb::SConnection *conn);
110
DRC77be9292015-02-21 11:44:26 -0600111 void getStats(double&, unsigned long long&, unsigned long long&);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100112};
113
114class SConn : public rfb::SConnection {
115public:
116 SConn();
117 ~SConn();
118
119 void writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb);
120
DRC77be9292015-02-21 11:44:26 -0600121 void getStats(double&, unsigned long long&, unsigned long long&);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100122
123 virtual void setAccessRights(AccessRights ar);
124
125 virtual void setDesktopSize(int fb_width, int fb_height,
126 const rfb::ScreenSet& layout);
127
128protected:
129 DummyOutStream *out;
130 Manager *manager;
131};
132
133DummyOutStream::DummyOutStream()
134{
135 offset = 0;
136 ptr = buf;
137 end = buf + sizeof(buf);
138}
139
140int DummyOutStream::length()
141{
142 flush();
143 return offset;
144}
145
146void DummyOutStream::flush()
147{
148 offset += ptr - buf;
149 ptr = buf;
150}
151
152int DummyOutStream::overrun(int itemSize, int nItems)
153{
154 flush();
155}
156
157CConn::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));
DRCb4c4a382015-02-21 11:28:37 -0600168 for (i = 0; i < rfb::encodingMax; i++) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100169 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());
DRCb4c4a382015-02-21 11:28:37 -0600189 sc->setEncodings(sizeof(encodings) / sizeof(*encodings), encodings);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100190}
191
192CConn::~CConn()
193{
194 int i;
195
196 delete sc;
197
198 delete in;
199
DRCb4c4a382015-02-21 11:28:37 -0600200 for (i = 0; i < rfb::encodingMax; i++)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100201 delete decoders[i];
202}
203
DRC77be9292015-02-21 11:44:26 -0600204void CConn::getStats(double& ratio, unsigned long long& bytes,
205 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100206{
DRC77be9292015-02-21 11:44:26 -0600207 sc->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100208}
209
210void CConn::setDesktopSize(int w, int h)
211{
212 CConnection::setDesktopSize(w, h);
213
214 pb.setSize(cp.width, cp.height);
215}
216
217void CConn::setCursor(int, int, const rfb::Point&, void*, void*)
218{
219}
220
221void CConn::framebufferUpdateStart()
222{
223 updates.clear();
224}
225
226void 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
240void 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
255void CConn::setColourMapEntries(int, int, rdr::U16*)
256{
257}
258
259void CConn::bell()
260{
261}
262
263void CConn::serverCutText(const char*, rdr::U32)
264{
265}
266
267Manager::Manager(class rfb::SConnection *conn) :
268 EncodeManager(conn)
269{
270}
271
DRC77be9292015-02-21 11:44:26 -0600272void Manager::getStats(double& ratio, unsigned long long& encodedBytes,
273 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100274{
275 StatsVector::iterator iter;
276 unsigned long long bytes, equivalent;
277
278 bytes = equivalent = 0;
DRCb4c4a382015-02-21 11:28:37 -0600279 for (iter = stats.begin(); iter != stats.end(); ++iter) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100280 StatsVector::value_type::iterator iter2;
DRCb4c4a382015-02-21 11:28:37 -0600281 for (iter2 = iter->begin(); iter2 != iter->end(); ++iter2) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100282 bytes += iter2->bytes;
283 equivalent += iter2->equivalent;
284 }
285 }
286
DRC77be9292015-02-21 11:44:26 -0600287 ratio = (double)equivalent / bytes;
288 encodedBytes = bytes;
289 rawEquivalent = equivalent;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100290}
291
292SConn::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
302SConn::~SConn()
303{
304 delete manager;
305 delete out;
306}
307
308void SConn::writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb)
309{
310 manager->writeUpdate(ui, pb, NULL);
311}
312
DRC77be9292015-02-21 11:44:26 -0600313void SConn::getStats(double& ratio, unsigned long long& bytes,
314 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100315{
DRC77be9292015-02-21 11:44:26 -0600316 manager->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100317}
318
319void SConn::setAccessRights(AccessRights ar)
320{
321}
322
323void SConn::setDesktopSize(int fb_width, int fb_height,
324 const rfb::ScreenSet& layout)
325{
326}
327
DRC77be9292015-02-21 11:44:26 -0600328static double runTest(const char *fn, double& ratio, unsigned long long& bytes,
329 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100330{
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;
DRC77be9292015-02-21 11:44:26 -0600346 cc->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100347
348 delete cc;
349
350 return time;
351}
352
353static void sort(double *array, int count)
354{
355 bool sorted;
356 int i;
357 do {
358 sorted = true;
DRCb4c4a382015-02-21 11:28:37 -0600359 for (i = 1; i < count; i++) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100360 if (array[i-1] > array[i]) {
361 double d;
362 d = array[i];
DRCb4c4a382015-02-21 11:28:37 -0600363 array[i] = array[i - 1];
364 array[i - 1] = d;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100365 sorted = false;
366 }
367 }
368 } while (!sorted);
369}
370
371static const int runCount = 9;
372
373static 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
381int 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;
DRC77be9292015-02-21 11:44:26 -0600389 unsigned long long bytes, equivalent;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100390
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] == '-') {
DRCb4c4a382015-02-21 11:28:37 -0600397 if (i + 1 < argc) {
398 if (rfb::Configuration::setParam(&argv[i][1], argv[i + 1])) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100399 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
DRC77be9292015-02-21 11:44:26 -0600428 runTest(fn, ratio, bytes, equivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100429
430 // Multiple runs to get a good average
DRCb4c4a382015-02-21 11:28:37 -0600431 for (i = 0; i < runCount; i++)
DRC77be9292015-02-21 11:44:26 -0600432 times[i] = runTest(fn, ratio, bytes, equivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100433
434 // Calculate median and median deviation
435 sort(times, runCount);
DRCb4c4a382015-02-21 11:28:37 -0600436 median = times[runCount / 2];
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100437
DRCb4c4a382015-02-21 11:28:37 -0600438 for (i = 0; i < runCount; i++)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100439 dev[i] = fabs((times[i] - median) / median) * 100;
440
441 sort(dev, runCount);
DRCb4c4a382015-02-21 11:28:37 -0600442 meddev = dev[runCount / 2];
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100443
444 printf("CPU time: %g s (+/- %g %)\n", median, meddev);
DRC77be9292015-02-21 11:44:26 -0600445 printf("Encoded bytes: %lld\n", bytes);
446 printf("Raw equivalent bytes: %lld\n", equivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100447 printf("Ratio: %g\n", ratio);
448
449 return 0;
450}