Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ConnectionIndicator.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. this.jid + "')\" data-i18n='connectionindicator." +
  145. (this.showMoreValue ? "less" : "more") + "'>" +
  146. translate("connectionindicator." + (this.showMoreValue ? "less" : "more")) +
  147. "</div><br />";
  148. }
  149. if(this.showMoreValue)
  150. {
  151. var downloadBandwidth, uploadBandwidth, transport;
  152. if(this.bandwidth === null)
  153. {
  154. downloadBandwidth = "N/A";
  155. uploadBandwidth = "N/A";
  156. }
  157. else
  158. {
  159. downloadBandwidth = this.bandwidth.download?
  160. this.bandwidth.download + " Kbps" :
  161. "N/A";
  162. uploadBandwidth = this.bandwidth.upload?
  163. this.bandwidth.upload + " Kbps" :
  164. "N/A";
  165. }
  166. if(!this.transport || this.transport.length === 0)
  167. {
  168. transport = "<tr>" +
  169. "<td><span class='jitsipopover_blue' " +
  170. "data-i18n='connectionindicator.address'>" +
  171. translate("connectionindicator.address") + "</span></td>" +
  172. "<td> N/A</td></tr>";
  173. }
  174. else
  175. {
  176. var data = {remoteIP: [], localIP:[], remotePort:[], localPort:[]};
  177. for(i = 0; i < this.transport.length; i++)
  178. {
  179. var ip = ConnectionIndicator.getIP(this.transport[i].ip);
  180. var port = ConnectionIndicator.getPort(this.transport[i].ip);
  181. var localIP =
  182. ConnectionIndicator.getIP(this.transport[i].localip);
  183. var localPort =
  184. ConnectionIndicator.getPort(this.transport[i].localip);
  185. if(data.remoteIP.indexOf(ip) == -1)
  186. {
  187. data.remoteIP.push(ip);
  188. }
  189. if(data.remotePort.indexOf(port) == -1)
  190. {
  191. data.remotePort.push(port);
  192. }
  193. if(data.localIP.indexOf(localIP) == -1)
  194. {
  195. data.localIP.push(localIP);
  196. }
  197. if(data.localPort.indexOf(localPort) == -1)
  198. {
  199. data.localPort.push(localPort);
  200. }
  201. }
  202. var local_address_key = "connectionindicator.localaddress";
  203. var remote_address_key = "connectionindicator.remoteaddress";
  204. var localTransport =
  205. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  206. local_address_key +"' data-i18n-options='" +
  207. JSON.stringify({count: data.localIP.length}) + "'>" +
  208. translate(local_address_key, {count: data.localIP.length}) +
  209. "</span></td><td> " +
  210. ConnectionIndicator.getStringFromArray(data.localIP) +
  211. "</td></tr>";
  212. transport =
  213. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  214. remote_address_key + "' data-i18n-options='" +
  215. JSON.stringify({count: data.remoteIP.length}) + "'>" +
  216. translate(remote_address_key,
  217. {count: data.remoteIP.length}) +
  218. "</span></td><td> " +
  219. ConnectionIndicator.getStringFromArray(data.remoteIP) +
  220. "</td></tr>";
  221. var key_remote = "connectionindicator.remoteport",
  222. key_local = "connectionindicator.localport";
  223. transport += "<tr>" +
  224. "<td>" +
  225. "<span class='jitsipopover_blue' data-i18n='" + key_remote +
  226. "' data-i18n-options='" +
  227. JSON.stringify({count: this.transport.length}) + "'>" +
  228. translate(key_remote, {count: this.transport.length}) +
  229. "</span></td><td>";
  230. localTransport += "<tr>" +
  231. "<td>" +
  232. "<span class='jitsipopover_blue' data-i18n='" + key_local +
  233. "' data-i18n-options='" +
  234. JSON.stringify({count: this.transport.length}) + "'>" +
  235. translate(key_local, {count: this.transport.length}) +
  236. "</span></td><td>";
  237. transport +=
  238. ConnectionIndicator.getStringFromArray(data.remotePort);
  239. localTransport +=
  240. ConnectionIndicator.getStringFromArray(data.localPort);
  241. transport += "</td></tr>";
  242. transport += localTransport + "</td></tr>";
  243. transport +="<tr>" +
  244. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.transport'>" +
  245. translate("connectionindicator.transport") + "</span></td>" +
  246. "<td>" + this.transport[0].type + "</td></tr>";
  247. }
  248. result += "<table style='width:100%'>" +
  249. "<tr>" +
  250. "<td>" +
  251. "<span class='jitsipopover_blue' data-i18n='connectionindicator.bandwidth'>" +
  252. translate("connectionindicator.bandwidth") + "</span>" +
  253. "</td><td>" +
  254. "<span class='jitsipopover_green'>&darr;</span>" +
  255. downloadBandwidth +
  256. " <span class='jitsipopover_orange'>&uarr;</span>" +
  257. uploadBandwidth + "</td></tr>";
  258. result += transport + "</table>";
  259. }
  260. return result;
  261. };
  262. /**
  263. * Shows or hide the additional information.
  264. */
  265. ConnectionIndicator.prototype.showMore = function () {
  266. this.showMoreValue = !this.showMoreValue;
  267. this.updatePopoverData();
  268. };
  269. function createIcon(classes)
  270. {
  271. var icon = document.createElement("span");
  272. for(var i in classes)
  273. {
  274. icon.classList.add(classes[i]);
  275. }
  276. icon.appendChild(
  277. document.createElement("i")).classList.add("icon-connection");
  278. return icon;
  279. }
  280. /**
  281. * Creates the indicator
  282. */
  283. ConnectionIndicator.prototype.create = function () {
  284. this.connectionIndicatorContainer = document.createElement("div");
  285. this.connectionIndicatorContainer.className = "connectionindicator";
  286. this.connectionIndicatorContainer.style.display = "none";
  287. this.videoContainer.container.appendChild(this.connectionIndicatorContainer);
  288. this.popover = new JitsiPopover(
  289. $("#" + this.videoContainer.videoSpanId + " > .connectionindicator"),
  290. {content: "<div class=\"connection_info\" data-i18n='connectionindicator.na'>" +
  291. APP.translation.translateString("connectionindicator.na") + "</div>",
  292. skin: "black"});
  293. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  294. createIcon(["connection", "connection_empty"]));
  295. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  296. createIcon(["connection", "connection_full"]));
  297. };
  298. /**
  299. * Removes the indicator
  300. */
  301. ConnectionIndicator.prototype.remove = function()
  302. {
  303. if (this.connectionIndicatorContainer.parentNode) {
  304. this.connectionIndicatorContainer.parentNode.removeChild(
  305. this.connectionIndicatorContainer);
  306. }
  307. this.popover.forceHide();
  308. };
  309. /**
  310. * Updates the data of the indicator
  311. * @param percent the percent of connection quality
  312. * @param object the statistics data.
  313. */
  314. ConnectionIndicator.prototype.updateConnectionQuality =
  315. function (percent, object) {
  316. if(percent === null)
  317. {
  318. this.connectionIndicatorContainer.style.display = "none";
  319. this.popover.forceHide();
  320. return;
  321. }
  322. else
  323. {
  324. if(this.connectionIndicatorContainer.style.display == "none") {
  325. this.connectionIndicatorContainer.style.display = "block";
  326. this.videoContainer.updateIconPositions();
  327. }
  328. }
  329. this.bandwidth = object.bandwidth;
  330. this.bitrate = object.bitrate;
  331. this.packetLoss = object.packetLoss;
  332. this.transport = object.transport;
  333. if(object.resolution)
  334. {
  335. this.resolution = object.resolution;
  336. }
  337. for(var quality in ConnectionIndicator.connectionQualityValues)
  338. {
  339. if(percent >= quality)
  340. {
  341. this.fullIcon.style.width =
  342. ConnectionIndicator.connectionQualityValues[quality];
  343. }
  344. }
  345. this.updatePopoverData();
  346. };
  347. /**
  348. * Updates the resolution
  349. * @param resolution the new resolution
  350. */
  351. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  352. this.resolution = resolution;
  353. this.updatePopoverData();
  354. };
  355. /**
  356. * Updates the content of the popover
  357. */
  358. ConnectionIndicator.prototype.updatePopoverData = function () {
  359. this.popover.updateContent(
  360. "<div class=\"connection_info\">" + this.generateText() + "</div>");
  361. APP.translation.translateElement($(".connection_info"));
  362. };
  363. /**
  364. * Hides the popover
  365. */
  366. ConnectionIndicator.prototype.hide = function () {
  367. this.popover.forceHide();
  368. };
  369. /**
  370. * Hides the indicator
  371. */
  372. ConnectionIndicator.prototype.hideIndicator = function () {
  373. this.connectionIndicatorContainer.style.display = "none";
  374. if(this.popover)
  375. this.popover.forceHide();
  376. };
  377. module.exports = ConnectionIndicator;