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

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