]> git.proxmox.com Git - mirror_novnc.git/blame - tests/playback-ui.js
Move cursor URI check to RFB object
[mirror_novnc.git] / tests / playback-ui.js
CommitLineData
d6c17390
SR
1import * as WebUtil from '../app/webutil.js';
2import RecordingPlayer from './playback.js';
3
4var frames = null;
5var encoding = null;
6
7function message(str) {
8 console.log(str);
9 var cell = document.getElementById('messages');
10 cell.textContent += str + "\n";
11 cell.scrollTop = cell.scrollHeight;
12}
13
14function 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 32function 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
55const notification = function (rfb, mesg, level, options) {
56 document.getElementById('VNC_status').textContent = mesg;
57}
58
59function IterationPlayer (iterations, frames, encoding) {
60 this._iterations = iterations;
61
62 this._iteration = undefined;
63 this._player = undefined;
64
65 this._start_time = undefined;
66
67 this._frames = frames;
68 this._encoding = encoding;
69
70 this._state = 'running';
71
72 this.onfinish = function() {};
73 this.oniterationfinish = function() {};
74 this.rfbdisconnected = function() {};
75 this.rfbnotification = function() {};
76}
77
78IterationPlayer.prototype = {
79 start: function (mode) {
80 this._iteration = 0;
81 this._start_time = (new Date()).getTime();
82
83 this._realtime = mode.startsWith('realtime');
84 this._trafficMgmt = !mode.endsWith('-no-mgmt');
85
86 this._nextIteration();
87 },
88
89 _nextIteration: function () {
90 const player = new RecordingPlayer(this._frames, this._encoding, this._disconnected.bind(this), this._notification.bind(this));
91 player.onfinish = this._iterationFinish.bind(this);
92
93 if (this._state !== 'running') { return; }
94
95 this._iteration++;
96 if (this._iteration > this._iterations) {
97 this._finish();
98 return;
99 }
100
101 player.run(this._realtime, this._trafficMgmt);
102 },
103
104 _finish: function () {
105 const endTime = (new Date()).getTime();
106 const totalDuration = endTime - this._start_time;
107
108 const evt = new Event('finish');
109 evt.duration = totalDuration;
110 evt.iterations = this._iterations;
111 this.onfinish(evt);
112 },
113
114 _iterationFinish: function (duration) {
115 const evt = new Event('iterationfinish');
116 evt.duration = duration;
117 evt.number = this._iteration;
118 this.oniterationfinish(evt);
119
120 this._nextIteration();
121 },
122
123 _disconnected: function (rfb, reason, frame) {
124 if (reason) {
125 this._state = 'failed';
126 }
127
128 var evt = new Event('rfbdisconnected');
129 evt.reason = reason;
130 evt.frame = frame;
131
132 this.onrfbdisconnected(evt);
133 },
134
135 _notification: function (rfb, msg, level, options) {
136 var evt = new Event('rfbnotification');
137 evt.message = msg;
138 evt.level = level;
139 evt.options = options;
140
141 this.onrfbnotification(evt);
142 },
143};
144
145function start() {
146 document.getElementById('startButton').value = "Running";
147 document.getElementById('startButton').disabled = true;
148
149 const iterations = document.getElementById('iterations').value;
150
151 var mode;
152
153 if (document.getElementById('mode1').checked) {
154 message(`Starting performance playback (fullspeed) [${iterations} iteration(s)]`);
155 mode = 'perftest';
156 } else {
157 message(`Starting realtime playback [${iterations} iteration(s)]`);
158 mode = 'realtime';
159 }
160
161 const player = new IterationPlayer(iterations, frames, encoding);
162 player.oniterationfinish = function (evt) {
163 message(`Iteration ${evt.number} took ${evt.duration}ms`);
164 };
165 player.onrfbdisconnected = function (evt) {
166 if (evt.reason) {
167 message(`noVNC sent disconnected during iteration ${evt.iteration} frame ${evt.frame}`);
168 }
169 };
170 player.onrfbnotification = function (evt) {
171 document.getElementById('VNC_status').textContent = evt.message;
172 };
173 player.onfinish = function (evt) {
174 const iterTime = parseInt(evt.duration / evt.iterations, 10);
175 message(`${evt.iterations} iterations took ${evt.duration}ms (average ${iterTime}ms / iteration)`);
176
177 document.getElementById('startButton').disabled = false;
178 document.getElementById('startButton').value = "Start";
179 };
180 player.start(mode);
181}
182
0ff97c1a 183loadFile().then(enableUI).catch(function (e) { message("Error loading recording: " + e); });