Files
noVNC/tests/playback.js
T

203 lines
5.9 KiB
JavaScript
Raw Normal View History

2010-09-08 10:11:11 -05:00
/*
* noVNC: HTML5 VNC client
2018-10-09 06:15:35 -04:00
* Copyright (C) 2018 The noVNC Authors
* Licensed under MPL 2.0 (see LICENSE.txt)
2010-09-08 10:11:11 -05:00
*/
2017-03-01 21:10:09 -05:00
import RFB from '../core/rfb.js';
import * as Log from '../core/util/logging.js';
import Base64 from '../core/base64.js';
2016-10-24 17:37:21 +02:00
// Immediate polyfill
2018-05-24 00:27:09 +03:00
if (window.setImmediate === undefined) {
let _immediateIdCounter = 1;
const _immediateFuncs = {};
2016-10-24 17:37:21 +02:00
window.setImmediate = (func) => {
2018-05-24 00:27:09 +03:00
const index = _immediateIdCounter++;
2016-10-24 17:37:21 +02:00
_immediateFuncs[index] = func;
window.postMessage("noVNC immediate trigger:" + index, "*");
return index;
};
window.clearImmediate = (id) => {
2016-10-24 17:37:21 +02:00
_immediateFuncs[id];
};
window.addEventListener("message", (event) => {
2016-10-24 17:37:21 +02:00
if ((typeof event.data !== "string") ||
(event.data.indexOf("noVNC immediate trigger:") !== 0)) {
return;
}
2018-05-24 00:27:09 +03:00
const index = event.data.slice("noVNC immediate trigger:".length);
2016-10-24 17:37:21 +02:00
2018-05-24 00:27:09 +03:00
const callback = _immediateFuncs[index];
2016-10-24 17:37:21 +02:00
if (callback === undefined) {
return;
}
delete _immediateFuncs[index];
callback();
});
2016-10-24 17:37:21 +02:00
}
2018-07-05 21:31:56 +02:00
export default class RecordingPlayer {
constructor(frames, encoding, disconnected) {
this._frames = frames;
this._encoding = encoding;
2015-05-15 14:27:23 -04:00
2018-07-05 21:31:56 +02:00
this._disconnected = disconnected;
2015-05-15 14:27:23 -04:00
2018-07-05 21:31:56 +02:00
if (this._encoding === undefined) {
2018-09-06 16:17:43 +02:00
const frame = this._frames[0];
const start = frame.indexOf('{', 1) + 1;
2018-07-05 21:31:56 +02:00
if (frame.slice(start).startsWith('UkZC')) {
this._encoding = 'base64';
} else {
this._encoding = 'binary';
}
2016-10-05 10:20:17 +02:00
}
2018-07-05 21:31:56 +02:00
this._rfb = undefined;
this._frame_length = this._frames.length;
2018-07-05 21:31:56 +02:00
this._frame_index = 0;
this._start_time = undefined;
this._realtime = true;
this._trafficManagement = true;
2018-07-05 21:31:56 +02:00
this._running = false;
this.onfinish = () => {};
2018-07-05 21:31:56 +02:00
}
2018-07-05 21:31:56 +02:00
run(realtime, trafficManagement) {
2017-03-01 21:10:09 -05:00
// initialize a new RFB
this._rfb = new RFB(document.getElementById('VNC_screen'), 'wss://test');
2017-10-14 15:39:56 +02:00
this._rfb.viewOnly = true;
this._rfb.addEventListener("disconnect",
this._handleDisconnect.bind(this));
this._rfb.addEventListener("credentialsrequired",
this._handleCredentials.bind(this));
2017-03-01 21:10:09 -05:00
this._enablePlaybackMode();
// reset the frame index and timer
this._frame_index = 0;
this._start_time = (new Date()).getTime();
this._realtime = realtime;
this._trafficManagement = (trafficManagement === undefined) ? !realtime : trafficManagement;
this._running = true;
2018-07-05 21:31:56 +02:00
}
2017-03-01 21:10:09 -05:00
// _enablePlaybackMode mocks out things not required for running playback
2018-07-05 21:31:56 +02:00
_enablePlaybackMode() {
2018-07-29 19:14:56 -04:00
const self = this;
this._rfb._sock.send = () => {};
this._rfb._sock.close = () => {};
this._rfb._sock.flush = () => {};
this._rfb._sock.open = function () {
this.init();
this._eventHandlers.open();
2018-07-29 19:14:56 -04:00
self._queueNextPacket();
2017-03-01 21:10:09 -05:00
};
2018-07-05 21:31:56 +02:00
}
2017-03-01 21:10:09 -05:00
2018-07-05 21:31:56 +02:00
_queueNextPacket() {
2017-03-01 21:10:09 -05:00
if (!this._running) { return; }
2018-05-24 00:27:09 +03:00
let frame = this._frames[this._frame_index];
2017-03-01 21:10:09 -05:00
// skip send frames
while (this._frame_index < this._frame_length && frame.charAt(0) === "}") {
this._frame_index++;
frame = this._frames[this._frame_index];
}
2017-03-01 21:10:09 -05:00
if (frame === 'EOF') {
Log.Debug('Finished, found EOF');
this._finish();
return;
}
2017-03-01 21:10:09 -05:00
if (this._frame_index >= this._frame_length) {
Log.Debug('Finished, no more frames');
this._finish();
return;
}
2017-03-01 21:10:09 -05:00
if (this._realtime) {
2018-05-24 00:27:09 +03:00
const foffset = frame.slice(1, frame.indexOf('{', 1));
const toffset = (new Date()).getTime() - this._start_time;
2017-03-01 21:10:09 -05:00
let delay = foffset - toffset;
if (delay < 1) delay = 1;
2017-03-01 21:10:09 -05:00
setTimeout(this._doPacket.bind(this), delay);
} else {
setImmediate(this._doPacket.bind(this));
}
2018-07-05 21:31:56 +02:00
}
2017-03-01 21:10:09 -05:00
2018-07-05 21:31:56 +02:00
_doPacket() {
2017-03-01 21:10:09 -05:00
// Avoid having excessive queue buildup in non-realtime mode
2017-06-01 12:50:00 +02:00
if (this._trafficManagement && this._rfb._flushing) {
2018-05-24 00:27:09 +03:00
const orig = this._rfb._display.onflush;
this._rfb._display.onflush = () => {
this._rfb._display.onflush = orig;
this._rfb._onFlush();
this._doPacket();
2017-10-14 15:39:56 +02:00
};
2017-03-01 21:10:09 -05:00
return;
}
2017-03-01 21:10:09 -05:00
const frame = this._frames[this._frame_index];
2018-05-24 00:27:09 +03:00
let start = frame.indexOf('{', 1) + 1;
let u8;
2017-03-01 21:10:09 -05:00
if (this._encoding === 'base64') {
2018-05-24 00:25:44 +03:00
u8 = Base64.decode(frame.slice(start));
2017-03-01 21:10:09 -05:00
start = 0;
} else {
2018-05-24 00:25:44 +03:00
u8 = new Uint8Array(frame.length - start);
2017-03-01 21:10:09 -05:00
for (let i = 0; i < frame.length - start; i++) {
u8[i] = frame.charCodeAt(start + i);
}
}
2017-03-01 21:10:09 -05:00
this._rfb._sock._recv_message({'data': u8});
this._frame_index++;
this._queueNextPacket();
2018-07-05 21:31:56 +02:00
}
2017-03-01 21:10:09 -05:00
_finish() {
if (this._rfb._display.pending()) {
this._rfb._display.onflush = () => {
if (this._rfb._flushing) {
this._rfb._onFlush();
2017-03-01 21:10:09 -05:00
}
this._finish();
2017-10-14 15:39:56 +02:00
};
2017-03-01 21:10:09 -05:00
this._rfb._display.flush();
} else {
this._running = false;
2018-01-26 11:07:58 +01:00
this._rfb._sock._eventHandlers.close({code: 1000, reason: ""});
delete this._rfb;
2017-03-01 21:10:09 -05:00
this.onfinish((new Date()).getTime() - this._start_time);
}
2018-07-05 21:31:56 +02:00
}
_handleDisconnect(evt) {
2017-03-01 21:10:09 -05:00
this._running = false;
this._disconnected(evt.detail.clean, this._frame_index);
2017-03-01 21:10:09 -05:00
}
_handleCredentials(evt) {
this._rfb.sendCredentials({"username": "Foo",
"password": "Bar",
"target": "Baz"});
}
2018-07-05 21:31:56 +02:00
}