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.
7 * Module to support debug overlay window with connection stats.
12 /** @suppress {duplicate} */
13 var remoting
= remoting
|| {};
17 * @implements {remoting.WindowShape.ClientUI}
18 * @param {Element} statsElement The HTML div to which to update stats.
20 remoting
.ConnectionStats = function(statsElement
) {
24 this.statsElement_
= statsElement
;
27 * @type {remoting.ClientSession.PerfStats}
30 this.mostRecent_
= null
32 remoting
.windowShape
.addCallback(this);
36 * @return {remoting.ClientSession.PerfStats} The most recently-set PerfStats,
37 * or null if update() has not yet been called.
39 remoting
.ConnectionStats
.prototype.mostRecent = function() {
40 return this.mostRecent_
;
44 * Show or hide the connection stats div.
46 remoting
.ConnectionStats
.prototype.toggle = function() {
47 this.statsElement_
.hidden
= !this.statsElement_
.hidden
;
51 * If the stats panel is visible, add its bounding rectangle to the specified
53 * @param {Array.<{left: number, top: number, width: number, height: number}>}
54 * rects List of rectangles.
57 remoting
.ConnectionStats
.prototype.addToRegion = function(rects
) {
58 if (!this.statsElement_
.hidden
) {
59 rects
.push(this.statsElement_
.getBoundingClientRect());
64 * Update the statistics panel.
65 * @param {remoting.ClientSession.PerfStats} stats The connection statistics.
67 remoting
.ConnectionStats
.prototype.update = function(stats
) {
68 this.mostRecent_
= stats
;
70 var videoBandwidth
= stats
.videoBandwidth
;
71 if (videoBandwidth
!= undefined) {
72 if (videoBandwidth
< 1024) {
74 } else if (videoBandwidth
< 1048576) {
76 videoBandwidth
= videoBandwidth
/ 1024;
77 } else if (videoBandwidth
< 1073741824) {
79 videoBandwidth
= videoBandwidth
/ 1048576;
82 videoBandwidth
= videoBandwidth
/ 1073741824;
87 * @param {number} value
88 * @param {string} units
89 * @return {string} Formatted number.
91 function formatStatNumber(value
, units
) {
92 if (value
!= undefined) {
93 return value
.toFixed(2) + ' ' + units
;
99 var statistics
= document
.getElementById('statistics');
100 this.statsElement_
.innerText
= (
101 'Bandwidth: ' + formatStatNumber(videoBandwidth
, units
) +
102 ', Frame Rate: ' + formatStatNumber(stats
.videoFrameRate
, 'fps') +
103 ', Capture: ' + formatStatNumber(stats
.captureLatency
, 'ms') +
104 ', Encode: ' + formatStatNumber(stats
.encodeLatency
, 'ms') +
105 ', Decode: ' + formatStatNumber(stats
.decodeLatency
, 'ms') +
106 ', Render: ' + formatStatNumber(stats
.renderLatency
, 'ms') +
107 ', Latency: ' + formatStatNumber(stats
.roundtripLatency
, 'ms'));
111 * Check for the debug toggle hot-key.
113 * @param {Event} event The keyboard event.
114 * @return {void} Nothing.
116 remoting
.ConnectionStats
.onKeydown = function(event
) {
117 var element
= /** @type {Element} */ (event
.target
);
118 if (element
.tagName
== 'INPUT' || element
.tagName
== 'TEXTAREA') {
121 if (String
.fromCharCode(event
.which
) == 'D') {
122 remoting
.stats
.toggle();
126 /** @type {remoting.ConnectionStats} */
127 remoting
.stats
= null;