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

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