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