]>
Commit | Line | Data |
---|---|---|
d6c17390 SR |
1 | import * as WebUtil from '../app/webutil.js'; |
2 | import RecordingPlayer from './playback.js'; | |
3 | ||
4 | var frames = null; | |
5 | var encoding = null; | |
6 | ||
7 | function message(str) { | |
8 | console.log(str); | |
9 | var cell = document.getElementById('messages'); | |
10 | cell.textContent += str + "\n"; | |
11 | cell.scrollTop = cell.scrollHeight; | |
12 | } | |
13 | ||
14 | function loadFile() { | |
15 | const fname = WebUtil.getQueryVar('data', null); | |
16 | ||
17 | if (!fname) { | |
18 | return Promise.reject("Must specify data=FOO in query string."); | |
19 | } | |
20 | ||
21 | message("Loading " + fname); | |
7c1f1a9c PO |
22 | |
23 | return new Promise(function (resolve, reject) { | |
24 | var script = document.createElement("script"); | |
25 | script.onload = resolve; | |
26 | script.onerror = reject; | |
27 | document.body.appendChild(script); | |
28 | script.src = "../recordings/" + fname; | |
29 | }); | |
d6c17390 SR |
30 | } |
31 | ||
7c1f1a9c | 32 | function enableUI() { |
d6c17390 SR |
33 | var iterations = WebUtil.getQueryVar('iterations', 3); |
34 | document.getElementById('iterations').value = iterations; | |
35 | ||
36 | var mode = WebUtil.getQueryVar('mode', 3); | |
37 | if (mode === 'realtime') { | |
38 | document.getElementById('mode2').checked = true; | |
39 | } else { | |
40 | document.getElementById('mode1').checked = true; | |
41 | } | |
42 | ||
7c1f1a9c | 43 | message("VNC_frame_data.length: " + VNC_frame_data.length); |
d6c17390 SR |
44 | |
45 | const startButton = document.getElementById('startButton'); | |
afb621d5 | 46 | startButton.disabled = false; |
d6c17390 SR |
47 | startButton.addEventListener('click', start); |
48 | ||
7c1f1a9c | 49 | frames = VNC_frame_data; |
b05a7b1d PO |
50 | // Only present in older recordings |
51 | if (window.VNC_frame_encoding) | |
52 | encoding = VNC_frame_encoding; | |
d6c17390 SR |
53 | } |
54 | ||
d6c17390 SR |
55 | function IterationPlayer (iterations, frames, encoding) { |
56 | this._iterations = iterations; | |
57 | ||
58 | this._iteration = undefined; | |
59 | this._player = undefined; | |
60 | ||
61 | this._start_time = undefined; | |
62 | ||
63 | this._frames = frames; | |
64 | this._encoding = encoding; | |
65 | ||
66 | this._state = 'running'; | |
67 | ||
68 | this.onfinish = function() {}; | |
69 | this.oniterationfinish = function() {}; | |
70 | this.rfbdisconnected = function() {}; | |
d6c17390 SR |
71 | } |
72 | ||
73 | IterationPlayer.prototype = { | |
74 | start: function (mode) { | |
75 | this._iteration = 0; | |
76 | this._start_time = (new Date()).getTime(); | |
77 | ||
78 | this._realtime = mode.startsWith('realtime'); | |
79 | this._trafficMgmt = !mode.endsWith('-no-mgmt'); | |
80 | ||
81 | this._nextIteration(); | |
82 | }, | |
83 | ||
84 | _nextIteration: function () { | |
5b20d338 | 85 | const player = new RecordingPlayer(this._frames, this._encoding, this._disconnected.bind(this)); |
d6c17390 SR |
86 | player.onfinish = this._iterationFinish.bind(this); |
87 | ||
88 | if (this._state !== 'running') { return; } | |
89 | ||
90 | this._iteration++; | |
91 | if (this._iteration > this._iterations) { | |
92 | this._finish(); | |
93 | return; | |
94 | } | |
95 | ||
96 | player.run(this._realtime, this._trafficMgmt); | |
97 | }, | |
98 | ||
99 | _finish: function () { | |
100 | const endTime = (new Date()).getTime(); | |
101 | const totalDuration = endTime - this._start_time; | |
102 | ||
103 | const evt = new Event('finish'); | |
104 | evt.duration = totalDuration; | |
105 | evt.iterations = this._iterations; | |
106 | this.onfinish(evt); | |
107 | }, | |
108 | ||
109 | _iterationFinish: function (duration) { | |
110 | const evt = new Event('iterationfinish'); | |
111 | evt.duration = duration; | |
112 | evt.number = this._iteration; | |
113 | this.oniterationfinish(evt); | |
114 | ||
115 | this._nextIteration(); | |
116 | }, | |
117 | ||
a92c3317 | 118 | _disconnected: function (clean, frame) { |
d472f3f1 | 119 | if (!clean) { |
d6c17390 SR |
120 | this._state = 'failed'; |
121 | } | |
122 | ||
123 | var evt = new Event('rfbdisconnected'); | |
d472f3f1 | 124 | evt.clean = clean; |
d6c17390 | 125 | evt.frame = frame; |
a92c3317 | 126 | evt.iteration = this._iteration; |
d6c17390 SR |
127 | |
128 | this.onrfbdisconnected(evt); | |
129 | }, | |
d6c17390 SR |
130 | }; |
131 | ||
132 | function start() { | |
133 | document.getElementById('startButton').value = "Running"; | |
134 | document.getElementById('startButton').disabled = true; | |
135 | ||
136 | const iterations = document.getElementById('iterations').value; | |
137 | ||
138 | var mode; | |
139 | ||
140 | if (document.getElementById('mode1').checked) { | |
141 | message(`Starting performance playback (fullspeed) [${iterations} iteration(s)]`); | |
142 | mode = 'perftest'; | |
143 | } else { | |
144 | message(`Starting realtime playback [${iterations} iteration(s)]`); | |
145 | mode = 'realtime'; | |
146 | } | |
147 | ||
148 | const player = new IterationPlayer(iterations, frames, encoding); | |
149 | player.oniterationfinish = function (evt) { | |
150 | message(`Iteration ${evt.number} took ${evt.duration}ms`); | |
151 | }; | |
152 | player.onrfbdisconnected = function (evt) { | |
a92c3317 | 153 | if (!evt.clean) { |
d6c17390 SR |
154 | message(`noVNC sent disconnected during iteration ${evt.iteration} frame ${evt.frame}`); |
155 | } | |
156 | }; | |
d6c17390 SR |
157 | player.onfinish = function (evt) { |
158 | const iterTime = parseInt(evt.duration / evt.iterations, 10); | |
159 | message(`${evt.iterations} iterations took ${evt.duration}ms (average ${iterTime}ms / iteration)`); | |
160 | ||
161 | document.getElementById('startButton').disabled = false; | |
162 | document.getElementById('startButton').value = "Start"; | |
163 | }; | |
164 | player.start(mode); | |
165 | } | |
166 | ||
0ff97c1a | 167 | loadFile().then(enableUI).catch(function (e) { message("Error loading recording: " + e); }); |