]> git.proxmox.com Git - mirror_novnc.git/blob - tests/playback.js
Fix traffic management in playback tests
[mirror_novnc.git] / tests / playback.js
1 /*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under MPL 2.0 (see LICENSE.txt)
5 */
6
7 import RFB from '../core/rfb.js';
8 import * as Log from '../core/util/logging.js';
9 import Base64 from '../core/base64.js';
10
11 // Immediate polyfill
12 if (setImmediate === undefined) {
13 var _immediateIdCounter = 1;
14 var _immediateFuncs = {};
15
16 var setImmediate = function (func) {
17 var index = _immediateIdCounter++;
18 _immediateFuncs[index] = func;
19 window.postMessage("noVNC immediate trigger:" + index, "*");
20 return index;
21 };
22
23 window.clearImmediate = function (id) {
24 _immediateFuncs[id];
25 };
26
27 var _onMessage = function (event) {
28 if ((typeof event.data !== "string") ||
29 (event.data.indexOf("noVNC immediate trigger:") !== 0)) {
30 return;
31 }
32
33 var index = event.data.slice("noVNC immediate trigger:".length);
34
35 var callback = _immediateFuncs[index];
36 if (callback === undefined) {
37 return;
38 }
39
40 delete _immediateFuncs[index];
41
42 callback();
43 };
44 window.addEventListener("message", _onMessage);
45 }
46
47 export default function RecordingPlayer (frames, encoding, disconnected, notification) {
48 this._frames = frames;
49 this._encoding = encoding;
50
51 this._disconnected = disconnected;
52 this._notification = notification;
53
54 if (this._encoding === undefined) {
55 let frame = this._frames[0];
56 let start = frame.indexOf('{', 1) + 1;
57 if (frame.slice(start).startsWith('UkZC')) {
58 this._encoding = 'base64';
59 } else {
60 this._encoding = 'binary';
61 }
62 }
63
64 this._rfb = undefined;
65 this._frame_length = this._frames.length;
66
67 this._frame_index = 0;
68 this._start_time = undefined;
69 this._realtime = true;
70 this._trafficManagement = true;
71
72 this._running = false;
73
74 this.onfinish = function () {};
75 }
76
77 RecordingPlayer.prototype = {
78 run: function (realtime, trafficManagement) {
79 // initialize a new RFB
80 this._rfb = new RFB({'target': document.getElementById('VNC_canvas'),
81 'view_only': true,
82 'onDisconnected': this._handleDisconnect.bind(this),
83 'onNotification': this._notification});
84 this._enablePlaybackMode();
85
86 // reset the frame index and timer
87 this._frame_index = 0;
88 this._start_time = (new Date()).getTime();
89
90 this._realtime = realtime;
91 this._trafficManagement = (trafficManagement === undefined) ? !realtime : trafficManagement;
92
93 this._running = true;
94
95 // launch the tests
96 this._rfb.connect('test', 0, 'bogus');
97
98 this._queueNextPacket();
99 },
100
101 // _enablePlaybackMode mocks out things not required for running playback
102 _enablePlaybackMode: function () {
103 this._rfb._sock.send = function (arr) {};
104 this._rfb._sock.close = function () {};
105 this._rfb._sock.flush = function () {};
106 this._rfb._checkEvents = function () {};
107 this._rfb.connect = function (host, port, password, path) {
108 this._rfb_host = host;
109 this._rfb_port = port;
110 this._rfb_password = (password !== undefined) ? password : "";
111 this._rfb_path = (path !== undefined) ? path : "";
112 this._sock.init('binary', 'ws');
113 this._rfb_connection_state = 'connecting';
114 this._rfb_init_state = 'ProtocolVersion';
115 };
116 },
117
118 _queueNextPacket: function () {
119 if (!this._running) { return; }
120
121 var frame = this._frames[this._frame_index];
122
123 // skip send frames
124 while (this._frame_index < this._frame_length && frame.charAt(0) === "}") {
125 this._frame_index++;
126 frame = this._frames[this._frame_index];
127 }
128
129 if (frame === 'EOF') {
130 Log.Debug('Finished, found EOF');
131 this._finish();
132 return;
133 }
134
135 if (this._frame_index >= this._frame_length) {
136 Log.Debug('Finished, no more frames');
137 this._finish();
138 return;
139 }
140
141 if (this._realtime) {
142 let foffset = frame.slice(1, frame.indexOf('{', 1));
143 let toffset = (new Date()).getTime() - this._start_time;
144 let delay = foffset - toffset;
145 if (delay < 1) delay = 1;
146
147 setTimeout(this._doPacket.bind(this), delay);
148 } else {
149 setImmediate(this._doPacket.bind(this));
150 }
151 },
152
153 _doPacket: function () {
154 // Avoid having excessive queue buildup in non-realtime mode
155 if (this._trafficManagement && this._rfb._flushing) {
156 let player = this;
157 let orig = this._rfb._display.get_onFlush();
158 this._rfb._display.set_onFlush(function () {
159 player._rfb._display.set_onFlush(orig);
160 player._rfb._onFlush();
161 player._doPacket();
162 });
163 return;
164 }
165
166 const frame = this._frames[this._frame_index];
167 var start = frame.indexOf('{', 1) + 1;
168 if (this._encoding === 'base64') {
169 var u8 = Base64.decode(frame.slice(start));
170 start = 0;
171 } else {
172 var u8 = new Uint8Array(frame.length - start);
173 for (let i = 0; i < frame.length - start; i++) {
174 u8[i] = frame.charCodeAt(start + i);
175 }
176 }
177
178 this._rfb._sock._recv_message({'data': u8});
179 this._frame_index++;
180
181 this._queueNextPacket();
182 },
183
184 _finish() {
185 if (this._rfb._display.pending()) {
186 var player = this;
187 this._rfb._display.set_onFlush(function () {
188 if (player._rfb._flushing) {
189 player._rfb._onFlush();
190 }
191 player._finish();
192 });
193 this._rfb._display.flush();
194 } else {
195 this._running = false;
196 this.onfinish((new Date()).getTime() - this._start_time);
197 }
198 },
199
200 _handleDisconnect(rfb, reason) {
201 this._running = false;
202 this._disconnected(rfb, reason, this._frame_index);
203 }
204 };