-
Notifications
You must be signed in to change notification settings - Fork 8
/
connection_stats.js
135 lines (119 loc) · 3.61 KB
/
connection_stats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview
* Module to support debug overlay window with connection stats.
*/
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
/**
* @constructor
* @implements {remoting.WindowShape.ClientUI}
* @param {Element} statsElement The HTML div to which to update stats.
*/
remoting.ConnectionStats = function(statsElement) {
/**
* @private
*/
this.statsElement_ = statsElement;
/**
* @type {remoting.ClientSession.PerfStats}
* @private
*/
this.mostRecent_ = null
remoting.windowShape.addCallback(this);
};
/**
* @return {remoting.ClientSession.PerfStats} The most recently-set PerfStats,
* or null if update() has not yet been called.
*/
remoting.ConnectionStats.prototype.mostRecent = function() {
return this.mostRecent_;
};
/**
* Show or hide the connection stats div.
*/
remoting.ConnectionStats.prototype.toggle = function() {
this.statsElement_.hidden = !this.statsElement_.hidden;
};
/**
* Show or hide the connection stats div.
* @param {boolean} show
*/
remoting.ConnectionStats.prototype.show = function(show) {
this.statsElement_.hidden = !show;
};
/**
* If the stats panel is visible, add its bounding rectangle to the specified
* region.
* @param {Array<{left: number, top: number, width: number, height: number}>}
* rects List of rectangles.
*/
remoting.ConnectionStats.prototype.addToRegion = function(rects) {
if (!this.statsElement_.hidden) {
rects.push(this.statsElement_.getBoundingClientRect());
}
};
/**
* Update the statistics panel.
* @param {remoting.ClientSession.PerfStats} stats The connection statistics.
*/
remoting.ConnectionStats.prototype.update = function(stats) {
this.mostRecent_ = stats;
var units = '';
var videoBandwidth = stats.videoBandwidth;
if (videoBandwidth != undefined) {
if (videoBandwidth < 1024) {
units = 'Bps';
} else if (videoBandwidth < 1048576) {
units = 'KiBps';
videoBandwidth = videoBandwidth / 1024;
} else if (videoBandwidth < 1073741824) {
units = 'MiBps';
videoBandwidth = videoBandwidth / 1048576;
} else {
units = 'GiBps';
videoBandwidth = videoBandwidth / 1073741824;
}
}
/**
* @param {number} value
* @param {string} units
* @return {string} Formatted number.
*/
function formatStatNumber(value, units) {
if (value != undefined) {
return value.toFixed(2) + ' ' + units;
} else {
return "n/a";
}
}
var statistics = document.getElementById('statistics');
this.statsElement_.innerText = (
'Bandwidth: ' + formatStatNumber(videoBandwidth, units) +
', Frame Rate: ' + formatStatNumber(stats.videoFrameRate, 'fps') +
', Capture: ' + formatStatNumber(stats.captureLatency, 'ms') +
', Encode: ' + formatStatNumber(stats.encodeLatency, 'ms') +
', Decode: ' + formatStatNumber(stats.decodeLatency, 'ms') +
', Render: ' + formatStatNumber(stats.renderLatency, 'ms') +
', Latency: ' + formatStatNumber(stats.roundtripLatency, 'ms'));
};
/**
* Check for the debug toggle hot-key.
*
* @param {Event} event The keyboard event.
* @return {void} Nothing.
*/
remoting.ConnectionStats.onKeydown = function(event) {
var element = /** @type {Element} */ (event.target);
if (element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') {
return;
}
if (String.fromCharCode(event.which) == 'D') {
remoting.stats.toggle();
}
};
/** @type {remoting.ConnectionStats} */
remoting.stats = null;