playback.js 2.44 KB
Newer Older
Joel Martin's avatar
Joel Martin committed
1 2
/*
 * noVNC: HTML5 VNC client
3
 * Copyright (C) 2012 Joel Martin
4
 * Licensed under MPL 2.0 (see LICENSE.txt)
Joel Martin's avatar
Joel Martin committed
5 6 7 8 9 10
 */

"use strict";
/*jslint browser: true, white: false */
/*global Util, VNC_frame_data, finish */

11
var rfb, mode, test_state, frame_idx, frame_length,
Joel Martin's avatar
Joel Martin committed
12 13 14 15
    iteration, iterations, istart_time,

    // Pre-declarations for jslint
    send_array, next_iteration, queue_next_packet, do_packet;
16 17 18 19

// Override send_array
send_array = function (arr) {
    // Stub out send_array
Joel Martin's avatar
Joel Martin committed
20
};
21

Joel Martin's avatar
Joel Martin committed
22
next_iteration = function () {
23 24 25 26 27 28 29 30 31
    if (iteration === 0) {
        frame_length = VNC_frame_data.length;
        test_state = 'running';
    } else {
        rfb.disconnect();
    }
    
    if (test_state !== 'running') { return; }

Joel Martin's avatar
Joel Martin committed
32
    iteration += 1;
33 34 35 36 37 38 39 40 41 42 43
    if (iteration > iterations) {
        finish();
        return;
    }

    frame_idx = 0;
    istart_time = (new Date()).getTime();
    rfb.connect('test', 0, "bogus");

    queue_next_packet();

Joel Martin's avatar
Joel Martin committed
44
};
45

Joel Martin's avatar
Joel Martin committed
46 47
queue_next_packet = function () {
    var frame, foffset, toffset, delay;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    if (test_state !== 'running') { return; }

    frame = VNC_frame_data[frame_idx];
    while ((frame_idx < frame_length) && (frame.charAt(0) === "}")) {
        //Util.Debug("Send frame " + frame_idx);
        frame_idx += 1;
        frame = VNC_frame_data[frame_idx];
    }

    if (frame === 'EOF') {
        Util.Debug("Finished, found EOF");
        next_iteration();
        return;
    }
    if (frame_idx >= frame_length) {
        Util.Debug("Finished, no more frames");
        next_iteration();
        return;
    }

    if (mode === 'realtime') {
        foffset = frame.slice(1, frame.indexOf('{', 1));
        toffset = (new Date()).getTime() - istart_time;
        delay = foffset - toffset;
        if (delay < 1) {
            delay = 1;
        }

        setTimeout(do_packet, delay);
    } else {
        setTimeout(do_packet, 1);
    }
Joel Martin's avatar
Joel Martin committed
80
};
81

82 83
var bytes_processed = 0;

Joel Martin's avatar
Joel Martin committed
84
do_packet = function () {
85
    //Util.Debug("Processing frame: " + frame_idx);
86 87 88 89 90 91 92 93 94 95 96 97
    var frame = VNC_frame_data[frame_idx],
        start = frame.indexOf('{', 1) + 1;
    bytes_processed += frame.length - start;
    if (VNC_frame_encoding === 'binary') {
        var u8 = new Uint8Array(frame.length - start);
        for (var i = 0; i < frame.length - start; i++) {
            u8[i] = frame.charCodeAt(start + i);
        }
        rfb.recv_message({'data' : u8});
    } else {
        rfb.recv_message({'data' : frame.slice(start)});
    }
98 99 100
    frame_idx += 1;

    queue_next_packet();
Joel Martin's avatar
Joel Martin committed
101
};
102