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 * @param {Element} statsElement The HTML div to which to update stats.
18 * @param {remoting.ClientPlugin} plugin
19 * @param {remoting.WindowShape=} opt_windowShape
21 * @implements {remoting.WindowShape.ClientUI}
22 * @implements {base.Disposable}
24 remoting.ConnectionStats = function(statsElement, plugin, opt_windowShape) {
26 this.statsElement_ = statsElement;
28 /** @private {remoting.ClientSession.PerfStats} */
29 this.mostRecent_ = null
32 this.plugin_ = plugin;
37 this.timer_ = new base.RepeatingTimer(function(){
38 that.update(plugin.getPerfStats());
42 this.windowShape_ = opt_windowShape;
43 if (this.windowShape_) {
44 this.windowShape_.registerClientUI(this);
48 remoting.ConnectionStats.prototype.dispose = function() {
49 base.dispose(this.timer_);
52 if (this.windowShape_) {
53 this.windowShape_.unregisterClientUI(this);
58 * @return {remoting.ClientSession.PerfStats} The most recently-set PerfStats,
59 * or null if update() has not yet been called.
61 remoting.ConnectionStats.prototype.mostRecent = function() {
62 return this.mostRecent_;
66 * Show or hide the connection stats div.
68 remoting.ConnectionStats.prototype.toggle = function() {
69 this.statsElement_.hidden = !this.statsElement_.hidden;
75 remoting.ConnectionStats.prototype.isVisible = function() {
76 return !this.statsElement_.hidden;
80 * Show or hide the connection stats div.
81 * @param {boolean} show
83 remoting.ConnectionStats.prototype.show = function(show) {
84 this.statsElement_.hidden = !show;
88 * If the stats panel is visible, add its bounding rectangle to the specified
90 * @param {Array<{left: number, top: number, width: number, height: number}>}
91 * rects List of rectangles.
94 remoting.ConnectionStats.prototype.addToRegion = function(rects) {
95 if (!this.statsElement_.hidden) {
96 rects.push(this.statsElement_.getBoundingClientRect());
101 * Update the statistics panel.
102 * @param {remoting.ClientSession.PerfStats} stats The connection statistics.
104 remoting.ConnectionStats.prototype.update = function(stats) {
105 this.mostRecent_ = stats;
107 var videoBandwidth = stats.videoBandwidth;
108 if (videoBandwidth != undefined) {
109 if (videoBandwidth < 1024) {
111 } else if (videoBandwidth < 1048576) {
113 videoBandwidth = videoBandwidth / 1024;
114 } else if (videoBandwidth < 1073741824) {
116 videoBandwidth = videoBandwidth / 1048576;
119 videoBandwidth = videoBandwidth / 1073741824;
124 * @param {number} value
125 * @param {string} units
126 * @return {string} Formatted number.
128 function formatStatNumber(value, units) {
129 if (value != undefined) {
130 return value.toFixed(2) + ' ' + units;
136 var statistics = document.getElementById('statistics');
137 this.statsElement_.innerText = (
138 'Bandwidth: ' + formatStatNumber(videoBandwidth, units) +
139 ', Frame Rate: ' + formatStatNumber(stats.videoFrameRate, 'fps') +
140 ', Capture: ' + formatStatNumber(stats.captureLatency, 'ms') +
141 ', Encode: ' + formatStatNumber(stats.encodeLatency, 'ms') +
142 ', Decode: ' + formatStatNumber(stats.decodeLatency, 'ms') +
143 ', Render: ' + formatStatNumber(stats.renderLatency, 'ms') +
144 ', Latency: ' + formatStatNumber(stats.roundtripLatency, 'ms'));