]>
Commit | Line | Data |
---|---|---|
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); | |
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 | }); | |
30 | } | |
31 | ||
32 | function enableUI() { | |
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 | ||
43 | message("VNC_frame_data.length: " + VNC_frame_data.length); | |
44 | ||
45 | const startButton = document.getElementById('startButton'); | |
46 | startButton.disabled = false; | |
47 | startButton.addEventListener('click', start); | |
48 | ||
49 | frames = VNC_frame_data; | |
50 | encoding = VNC_frame_encoding; | |
51 | } | |
52 | ||
53 | const notification = function (rfb, mesg, level, options) { | |
54 | document.getElementById('VNC_status').textContent = mesg; | |
55 | } | |
56 | ||
57 | function IterationPlayer (iterations, frames, encoding) { | |
58 | this._iterations = iterations; | |
59 | ||
60 | this._iteration = undefined; | |
61 | this._player = undefined; | |
62 | ||
63 | this._start_time = undefined; | |
64 | ||
65 | this._frames = frames; | |
66 | this._encoding = encoding; | |
67 | ||
68 | this._state = 'running'; | |
69 | ||
70 | this.onfinish = function() {}; | |
71 | this.oniterationfinish = function() {}; | |
72 | this.rfbdisconnected = function() {}; | |
73 | this.rfbnotification = function() {}; | |
74 | } | |
75 | ||
76 | IterationPlayer.prototype = { | |
77 | start: function (mode) { | |
78 | this._iteration = 0; | |
79 | this._start_time = (new Date()).getTime(); | |
80 | ||
81 | this._realtime = mode.startsWith('realtime'); | |
82 | this._trafficMgmt = !mode.endsWith('-no-mgmt'); | |
83 | ||
84 | this._nextIteration(); | |
85 | }, | |
86 | ||
87 | _nextIteration: function () { | |
88 | const player = new RecordingPlayer(this._frames, this._encoding, this._disconnected.bind(this), this._notification.bind(this)); | |
89 | player.onfinish = this._iterationFinish.bind(this); | |
90 | ||
91 | if (this._state !== 'running') { return; } | |
92 | ||
93 | this._iteration++; | |
94 | if (this._iteration > this._iterations) { | |
95 | this._finish(); | |
96 | return; | |
97 | } | |
98 | ||
99 | player.run(this._realtime, this._trafficMgmt); | |
100 | }, | |
101 | ||
102 | _finish: function () { | |
103 | const endTime = (new Date()).getTime(); | |
104 | const totalDuration = endTime - this._start_time; | |
105 | ||
106 | const evt = new Event('finish'); | |
107 | evt.duration = totalDuration; | |
108 | evt.iterations = this._iterations; | |
109 | this.onfinish(evt); | |
110 | }, | |
111 | ||
112 | _iterationFinish: function (duration) { | |
113 | const evt = new Event('iterationfinish'); | |
114 | evt.duration = duration; | |
115 | evt.number = this._iteration; | |
116 | this.oniterationfinish(evt); | |
117 | ||
118 | this._nextIteration(); | |
119 | }, | |
120 | ||
121 | _disconnected: function (rfb, reason, frame) { | |
122 | if (reason) { | |
123 | this._state = 'failed'; | |
124 | } | |
125 | ||
126 | var evt = new Event('rfbdisconnected'); | |
127 | evt.reason = reason; | |
128 | evt.frame = frame; | |
129 | ||
130 | this.onrfbdisconnected(evt); | |
131 | }, | |
132 | ||
133 | _notification: function (rfb, msg, level, options) { | |
134 | var evt = new Event('rfbnotification'); | |
135 | evt.message = msg; | |
136 | evt.level = level; | |
137 | evt.options = options; | |
138 | ||
139 | this.onrfbnotification(evt); | |
140 | }, | |
141 | }; | |
142 | ||
143 | function start() { | |
144 | document.getElementById('startButton').value = "Running"; | |
145 | document.getElementById('startButton').disabled = true; | |
146 | ||
147 | const iterations = document.getElementById('iterations').value; | |
148 | ||
149 | var mode; | |
150 | ||
151 | if (document.getElementById('mode1').checked) { | |
152 | message(`Starting performance playback (fullspeed) [${iterations} iteration(s)]`); | |
153 | mode = 'perftest'; | |
154 | } else { | |
155 | message(`Starting realtime playback [${iterations} iteration(s)]`); | |
156 | mode = 'realtime'; | |
157 | } | |
158 | ||
159 | const player = new IterationPlayer(iterations, frames, encoding); | |
160 | player.oniterationfinish = function (evt) { | |
161 | message(`Iteration ${evt.number} took ${evt.duration}ms`); | |
162 | }; | |
163 | player.onrfbdisconnected = function (evt) { | |
164 | if (evt.reason) { | |
165 | message(`noVNC sent disconnected during iteration ${evt.iteration} frame ${evt.frame}`); | |
166 | } | |
167 | }; | |
168 | player.onrfbnotification = function (evt) { | |
169 | document.getElementById('VNC_status').textContent = evt.message; | |
170 | }; | |
171 | player.onfinish = function (evt) { | |
172 | const iterTime = parseInt(evt.duration / evt.iterations, 10); | |
173 | message(`${evt.iterations} iterations took ${evt.duration}ms (average ${iterTime}ms / iteration)`); | |
174 | ||
175 | document.getElementById('startButton').disabled = false; | |
176 | document.getElementById('startButton').value = "Start"; | |
177 | }; | |
178 | player.start(mode); | |
179 | } | |
180 | ||
181 | loadFile().then(enableUI).catch(function (e) { message("Error loading recording"); }); |