1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @fileoverview
7 * Module to support debug overlay window with connection stats.
8 */
9
10'use strict';
11
12/** @suppress {duplicate} */
13var remoting = remoting || {};
14
15/**
16 * @constructor
17 * @param {Element} statsElement The HTML div to which to update stats.
18 */
19remoting.ConnectionStats = function(statsElement) {
20  this.statsElement = statsElement;
21};
22
23/**
24 * Show or hide the connection stats div.
25 */
26remoting.ConnectionStats.prototype.toggle = function() {
27  this.statsElement.hidden = !this.statsElement.hidden;
28};
29
30/**
31 * Update the statistics panel.
32 * @param {remoting.ClientSession.PerfStats} stats The connection statistics.
33 */
34remoting.ConnectionStats.prototype.update = function(stats) {
35  var units = '';
36  var videoBandwidth = stats.videoBandwidth;
37  if (videoBandwidth != undefined) {
38    if (videoBandwidth < 1024) {
39      units = 'Bps';
40    } else if (videoBandwidth < 1048576) {
41      units = 'KiBps';
42      videoBandwidth = videoBandwidth / 1024;
43    } else if (videoBandwidth < 1073741824) {
44      units = 'MiBps';
45      videoBandwidth = videoBandwidth / 1048576;
46    } else {
47      units = 'GiBps';
48      videoBandwidth = videoBandwidth / 1073741824;
49    }
50  }
51
52  /**
53   * @param {number} value
54   * @param {string} units
55   * @return {string} Formatted number.
56   */
57  function formatStatNumber(value, units) {
58    if (value != undefined) {
59      return value.toFixed(2) + ' ' + units;
60    } else {
61      return "n/a";
62    }
63  }
64
65  var statistics = document.getElementById('statistics');
66  this.statsElement.innerText = (
67      'Bandwidth: ' + formatStatNumber(videoBandwidth, units) +
68      ', Frame Rate: ' + formatStatNumber(stats.videoFrameRate, 'fps') +
69      ', Capture: ' + formatStatNumber(stats.captureLatency, 'ms') +
70      ', Encode: ' + formatStatNumber(stats.encodeLatency, 'ms') +
71      ', Decode: ' + formatStatNumber(stats.decodeLatency, 'ms') +
72      ', Render: ' + formatStatNumber(stats.renderLatency, 'ms') +
73      ', Latency: ' + formatStatNumber(stats.roundtripLatency, 'ms'));
74};
75
76/**
77 * Check for the debug toggle hot-key.
78 *
79 * @param {Event} event The keyboard event.
80 * @return {void} Nothing.
81 */
82remoting.ConnectionStats.onKeydown = function(event) {
83  var element = /** @type {Element} */ (event.target);
84  if (element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') {
85    return;
86  }
87  if (String.fromCharCode(event.which) == 'D') {
88    remoting.stats.toggle();
89  }
90};
91
92/** @type {remoting.ConnectionStats} */
93remoting.stats = null;
94