You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectionIndicator.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* global APP, $ */
  2. /* jshint -W101 */
  3. var JitsiPopover = require("../util/JitsiPopover");
  4. /**
  5. * Constructs new connection indicator.
  6. * @param videoContainer the video container associated with the indicator.
  7. * @constructor
  8. */
  9. function ConnectionIndicator(videoContainer, jid) {
  10. this.videoContainer = videoContainer;
  11. this.bandwidth = null;
  12. this.packetLoss = null;
  13. this.bitrate = null;
  14. this.showMoreValue = false;
  15. this.resolution = null;
  16. this.transport = [];
  17. this.popover = null;
  18. this.jid = jid;
  19. this.create();
  20. }
  21. /**
  22. * Values for the connection quality
  23. * @type {{98: string,
  24. * 81: string,
  25. * 64: string,
  26. * 47: string,
  27. * 30: string,
  28. * 0: string}}
  29. */
  30. ConnectionIndicator.connectionQualityValues = {
  31. 98: "18px", //full
  32. 81: "15px",//4 bars
  33. 64: "11px",//3 bars
  34. 47: "7px",//2 bars
  35. 30: "3px",//1 bar
  36. 0: "0px"//empty
  37. };
  38. ConnectionIndicator.getIP = function(value) {
  39. return value.substring(0, value.lastIndexOf(":"));
  40. };
  41. ConnectionIndicator.getPort = function(value) {
  42. return value.substring(value.lastIndexOf(":") + 1, value.length);
  43. };
  44. ConnectionIndicator.getStringFromArray = function (array) {
  45. var res = "";
  46. for(var i = 0; i < array.length; i++) {
  47. res += (i === 0? "" : ", ") + array[i];
  48. }
  49. return res;
  50. };
  51. /**
  52. * Generates the html content.
  53. * @returns {string} the html content.
  54. */
  55. ConnectionIndicator.prototype.generateText = function () {
  56. var downloadBitrate, uploadBitrate, packetLoss, resolution, i;
  57. var translate = APP.translation.translateString;
  58. if(this.bitrate === null) {
  59. downloadBitrate = "N/A";
  60. uploadBitrate = "N/A";
  61. }
  62. else {
  63. downloadBitrate =
  64. this.bitrate.download? this.bitrate.download + " Kbps" : "N/A";
  65. uploadBitrate =
  66. this.bitrate.upload? this.bitrate.upload + " Kbps" : "N/A";
  67. }
  68. if(this.packetLoss === null) {
  69. packetLoss = "N/A";
  70. } else {
  71. packetLoss = "<span class='jitsipopover_green'>&darr;</span>" +
  72. (this.packetLoss.download !== null ?
  73. this.packetLoss.download : "N/A") +
  74. "% <span class='jitsipopover_orange'>&uarr;</span>" +
  75. (this.packetLoss.upload !== null? this.packetLoss.upload : "N/A") +
  76. "%";
  77. }
  78. var resolutionValue = null;
  79. if(this.resolution && this.jid) {
  80. var keys = Object.keys(this.resolution);
  81. for(var ssrc in this.resolution) {
  82. resolutionValue = this.resolution[ssrc];
  83. }
  84. }
  85. if(this.jid === null) {
  86. resolution = "";
  87. if(this.resolution === null || !Object.keys(this.resolution) ||
  88. Object.keys(this.resolution).length === 0) {
  89. resolution = "N/A";
  90. } else {
  91. for (i in this.resolution) {
  92. resolutionValue = this.resolution[i];
  93. if (resolutionValue) {
  94. if (resolutionValue.height &&
  95. resolutionValue.width) {
  96. resolution += (resolution === "" ? "" : ", ") +
  97. resolutionValue.width + "x" +
  98. resolutionValue.height;
  99. }
  100. }
  101. }
  102. }
  103. } else if(!resolutionValue ||
  104. !resolutionValue.height ||
  105. !resolutionValue.width) {
  106. resolution = "N/A";
  107. } else {
  108. resolution = resolutionValue.width + "x" + resolutionValue.height;
  109. }
  110. var result = "<table style='width:100%'>" +
  111. "<tr>" +
  112. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.bitrate'>" +
  113. translate("connectionindicator.bitrate") + "</span></td>" +
  114. "<td><span class='jitsipopover_green'>&darr;</span>" +
  115. downloadBitrate + " <span class='jitsipopover_orange'>&uarr;</span>" +
  116. uploadBitrate + "</td>" +
  117. "</tr><tr>" +
  118. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.packetloss'>" +
  119. translate("connectionindicator.packetloss") + "</span></td>" +
  120. "<td>" + packetLoss + "</td>" +
  121. "</tr><tr>" +
  122. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.resolution'>" +
  123. translate("connectionindicator.resolution") + "</span></td>" +
  124. "<td>" + resolution + "</td></tr></table>";
  125. if(this.videoContainer.videoSpanId == "localVideoContainer") {
  126. result += "<div class=\"jitsipopover_showmore\" " +
  127. "onclick = \"APP.UI.connectionIndicatorShowMore('" +
  128. // FIXME: we do not know local jid when this text is generated
  129. //this.jid + "')\" data-i18n='connectionindicator." +
  130. "local')\" data-i18n='connectionindicator." +
  131. (this.showMoreValue ? "less" : "more") + "'>" +
  132. translate("connectionindicator." + (this.showMoreValue ? "less" : "more")) +
  133. "</div><br />";
  134. }
  135. if (this.showMoreValue) {
  136. var downloadBandwidth, uploadBandwidth, transport;
  137. if (this.bandwidth === null) {
  138. downloadBandwidth = "N/A";
  139. uploadBandwidth = "N/A";
  140. } else {
  141. downloadBandwidth = this.bandwidth.download?
  142. this.bandwidth.download + " Kbps" :
  143. "N/A";
  144. uploadBandwidth = this.bandwidth.upload?
  145. this.bandwidth.upload + " Kbps" :
  146. "N/A";
  147. }
  148. if (!this.transport || this.transport.length === 0) {
  149. transport = "<tr>" +
  150. "<td><span class='jitsipopover_blue' " +
  151. "data-i18n='connectionindicator.address'>" +
  152. translate("connectionindicator.address") + "</span></td>" +
  153. "<td> N/A</td></tr>";
  154. } else {
  155. var data = {remoteIP: [], localIP:[], remotePort:[], localPort:[]};
  156. for(i = 0; i < this.transport.length; i++) {
  157. var ip = ConnectionIndicator.getIP(this.transport[i].ip);
  158. var port = ConnectionIndicator.getPort(this.transport[i].ip);
  159. var localIP =
  160. ConnectionIndicator.getIP(this.transport[i].localip);
  161. var localPort =
  162. ConnectionIndicator.getPort(this.transport[i].localip);
  163. if(data.remoteIP.indexOf(ip) == -1) {
  164. data.remoteIP.push(ip);
  165. }
  166. if(data.remotePort.indexOf(port) == -1) {
  167. data.remotePort.push(port);
  168. }
  169. if(data.localIP.indexOf(localIP) == -1) {
  170. data.localIP.push(localIP);
  171. }
  172. if(data.localPort.indexOf(localPort) == -1) {
  173. data.localPort.push(localPort);
  174. }
  175. }
  176. var local_address_key = "connectionindicator.localaddress";
  177. var remote_address_key = "connectionindicator.remoteaddress";
  178. var localTransport =
  179. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  180. local_address_key +"' data-i18n-options='" +
  181. JSON.stringify({count: data.localIP.length}) + "'>" +
  182. translate(local_address_key, {count: data.localIP.length}) +
  183. "</span></td><td> " +
  184. ConnectionIndicator.getStringFromArray(data.localIP) +
  185. "</td></tr>";
  186. transport =
  187. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  188. remote_address_key + "' data-i18n-options='" +
  189. JSON.stringify({count: data.remoteIP.length}) + "'>" +
  190. translate(remote_address_key,
  191. {count: data.remoteIP.length}) +
  192. "</span></td><td> " +
  193. ConnectionIndicator.getStringFromArray(data.remoteIP) +
  194. "</td></tr>";
  195. var key_remote = "connectionindicator.remoteport",
  196. key_local = "connectionindicator.localport";
  197. transport += "<tr>" +
  198. "<td>" +
  199. "<span class='jitsipopover_blue' data-i18n='" + key_remote +
  200. "' data-i18n-options='" +
  201. JSON.stringify({count: this.transport.length}) + "'>" +
  202. translate(key_remote, {count: this.transport.length}) +
  203. "</span></td><td>";
  204. localTransport += "<tr>" +
  205. "<td>" +
  206. "<span class='jitsipopover_blue' data-i18n='" + key_local +
  207. "' data-i18n-options='" +
  208. JSON.stringify({count: this.transport.length}) + "'>" +
  209. translate(key_local, {count: this.transport.length}) +
  210. "</span></td><td>";
  211. transport +=
  212. ConnectionIndicator.getStringFromArray(data.remotePort);
  213. localTransport +=
  214. ConnectionIndicator.getStringFromArray(data.localPort);
  215. transport += "</td></tr>";
  216. transport += localTransport + "</td></tr>";
  217. transport +="<tr>" +
  218. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.transport'>" +
  219. translate("connectionindicator.transport") + "</span></td>" +
  220. "<td>" + this.transport[0].type + "</td></tr>";
  221. }
  222. result += "<table style='width:100%'>" +
  223. "<tr>" +
  224. "<td>" +
  225. "<span class='jitsipopover_blue' data-i18n='connectionindicator.bandwidth'>" +
  226. translate("connectionindicator.bandwidth") + "</span>" +
  227. "</td><td>" +
  228. "<span class='jitsipopover_green'>&darr;</span>" +
  229. downloadBandwidth +
  230. " <span class='jitsipopover_orange'>&uarr;</span>" +
  231. uploadBandwidth + "</td></tr>";
  232. result += transport + "</table>";
  233. }
  234. return result;
  235. };
  236. /**
  237. * Shows or hide the additional information.
  238. */
  239. ConnectionIndicator.prototype.showMore = function () {
  240. this.showMoreValue = !this.showMoreValue;
  241. this.updatePopoverData();
  242. };
  243. function createIcon(classes) {
  244. var icon = document.createElement("span");
  245. for(var i in classes) {
  246. icon.classList.add(classes[i]);
  247. }
  248. icon.appendChild(
  249. document.createElement("i")).classList.add("icon-connection");
  250. return icon;
  251. }
  252. /**
  253. * Creates the indicator
  254. */
  255. ConnectionIndicator.prototype.create = function () {
  256. this.connectionIndicatorContainer = document.createElement("div");
  257. this.connectionIndicatorContainer.className = "connectionindicator";
  258. this.connectionIndicatorContainer.style.display = "none";
  259. this.videoContainer.container.appendChild(
  260. this.connectionIndicatorContainer);
  261. this.popover = new JitsiPopover(
  262. $("#" + this.videoContainer.videoSpanId + " > .connectionindicator"),
  263. {content: "<div class=\"connection_info\" data-i18n='connectionindicator.na'>" +
  264. APP.translation.translateString("connectionindicator.na") + "</div>",
  265. skin: "black"});
  266. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  267. createIcon(["connection", "connection_empty"]));
  268. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  269. createIcon(["connection", "connection_full"]));
  270. };
  271. /**
  272. * Removes the indicator
  273. */
  274. ConnectionIndicator.prototype.remove = function() {
  275. if (this.connectionIndicatorContainer.parentNode) {
  276. this.connectionIndicatorContainer.parentNode.removeChild(
  277. this.connectionIndicatorContainer);
  278. }
  279. this.popover.forceHide();
  280. };
  281. /**
  282. * Updates the data of the indicator
  283. * @param percent the percent of connection quality
  284. * @param object the statistics data.
  285. */
  286. ConnectionIndicator.prototype.updateConnectionQuality =
  287. function (percent, object) {
  288. if (percent === null) {
  289. this.connectionIndicatorContainer.style.display = "none";
  290. this.popover.forceHide();
  291. return;
  292. } else {
  293. if(this.connectionIndicatorContainer.style.display == "none") {
  294. this.connectionIndicatorContainer.style.display = "block";
  295. this.videoContainer.updateIconPositions();
  296. }
  297. }
  298. this.bandwidth = object.bandwidth;
  299. this.bitrate = object.bitrate;
  300. this.packetLoss = object.packetLoss;
  301. this.transport = object.transport;
  302. if (object.resolution) {
  303. this.resolution = object.resolution;
  304. }
  305. for (var quality in ConnectionIndicator.connectionQualityValues) {
  306. if (percent >= quality) {
  307. this.fullIcon.style.width =
  308. ConnectionIndicator.connectionQualityValues[quality];
  309. }
  310. }
  311. this.updatePopoverData();
  312. };
  313. /**
  314. * Updates the resolution
  315. * @param resolution the new resolution
  316. */
  317. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  318. this.resolution = resolution;
  319. this.updatePopoverData();
  320. };
  321. /**
  322. * Updates the content of the popover
  323. */
  324. ConnectionIndicator.prototype.updatePopoverData = function () {
  325. this.popover.updateContent(
  326. "<div class=\"connection_info\">" + this.generateText() + "</div>");
  327. APP.translation.translateElement($(".connection_info"));
  328. };
  329. /**
  330. * Hides the popover
  331. */
  332. ConnectionIndicator.prototype.hide = function () {
  333. this.popover.forceHide();
  334. };
  335. /**
  336. * Hides the indicator
  337. */
  338. ConnectionIndicator.prototype.hideIndicator = function () {
  339. this.connectionIndicatorContainer.style.display = "none";
  340. if(this.popover)
  341. this.popover.forceHide();
  342. };
  343. module.exports = ConnectionIndicator;