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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /* global APP, $, config */
  2. /* jshint -W101 */
  3. import JitsiPopover from "../util/JitsiPopover";
  4. import VideoLayout from "./VideoLayout";
  5. /**
  6. * Constructs new connection indicator.
  7. * @param videoContainer the video container associated with the indicator.
  8. * @constructor
  9. */
  10. function ConnectionIndicator(videoContainer, id) {
  11. this.videoContainer = videoContainer;
  12. this.bandwidth = null;
  13. this.packetLoss = null;
  14. this.bitrate = null;
  15. this.showMoreValue = false;
  16. this.resolution = null;
  17. this.isResolutionHD = null;
  18. this.transport = [];
  19. this.popover = null;
  20. this.id = id;
  21. this.create();
  22. }
  23. /**
  24. * Values for the connection quality
  25. * @type {{98: string,
  26. * 81: string,
  27. * 64: string,
  28. * 47: string,
  29. * 30: string,
  30. * 0: string}}
  31. */
  32. ConnectionIndicator.connectionQualityValues = {
  33. 98: "18px", //full
  34. 81: "15px",//4 bars
  35. 64: "11px",//3 bars
  36. 47: "7px",//2 bars
  37. 30: "3px",//1 bar
  38. 0: "0px"//empty
  39. };
  40. ConnectionIndicator.getIP = function(value) {
  41. return value.substring(0, value.lastIndexOf(":"));
  42. };
  43. ConnectionIndicator.getPort = function(value) {
  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. 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, i;
  59. var translate = APP.translation.translateString;
  60. if(this.bitrate === null) {
  61. downloadBitrate = "N/A";
  62. uploadBitrate = "N/A";
  63. }
  64. else {
  65. downloadBitrate =
  66. this.bitrate.download? this.bitrate.download + " Kbps" : "N/A";
  67. uploadBitrate =
  68. this.bitrate.upload? this.bitrate.upload + " Kbps" : "N/A";
  69. }
  70. if(this.packetLoss === null) {
  71. packetLoss = "N/A";
  72. } else {
  73. packetLoss = "<span class='jitsipopover__green'>&darr;</span>" +
  74. (this.packetLoss.download !== null ?
  75. this.packetLoss.download : "N/A") +
  76. "% <span class='jitsipopover__orange'>&uarr;</span>" +
  77. (this.packetLoss.upload !== null? this.packetLoss.upload : "N/A") +
  78. "%";
  79. }
  80. // GENERATE RESOLUTIONS STRING
  81. let resolutions = this.resolution || {};
  82. let resolutionStr = Object.keys(resolutions).map(function (ssrc) {
  83. let {width, height} = resolutions[ssrc];
  84. return `${width}x${height}`;
  85. }).join(', ') || 'N/A';
  86. let headerKey = 'connectionindicator.header';
  87. let result = (
  88. `<table style='width:100%'>
  89. <tr>
  90. <td>
  91. <span class="jitsipopover__title"
  92. data-i18n="">${translate(headerKey)}</span>
  93. </td>
  94. </tr>
  95. <tr>
  96. <td>
  97. <span data-i18n='connectionindicator.bitrate'>
  98. ${translate("connectionindicator.bitrate")}
  99. </span>
  100. </td>
  101. <td>
  102. <span class='jitsipopover__green'>&darr;</span>
  103. ${downloadBitrate}
  104. <span class='jitsipopover__orange'>&uarr;</span>
  105. ${uploadBitrate}
  106. </td>
  107. </tr>
  108. <tr>
  109. <td>
  110. <span data-i18n='connectionindicator.packetloss'>
  111. ${translate("connectionindicator.packetloss")}
  112. </span>
  113. </td>
  114. <td>${packetLoss}</td>
  115. </tr>
  116. <tr>
  117. <td>
  118. <span data-i18n='connectionindicator.resolution'>
  119. ${translate("connectionindicator.resolution")}
  120. </span>
  121. </td>
  122. <td>
  123. ${resolutionStr}
  124. </td>
  125. </tr>
  126. </table>`);
  127. if(this.videoContainer.videoSpanId == "localVideoContainer") {
  128. result += "<a class=\"jitsipopover__showmore link\" " +
  129. "onclick = \"APP.UI.connectionIndicatorShowMore('" +
  130. // FIXME: we do not know local id when this text is generated
  131. //this.id + "')\" data-i18n='connectionindicator." +
  132. "local')\" data-i18n='connectionindicator." +
  133. (this.showMoreValue ? "less" : "more") + "'>" +
  134. translate("connectionindicator." + (this.showMoreValue ? "less" : "more")) +
  135. "</a>";
  136. }
  137. if (this.showMoreValue) {
  138. var downloadBandwidth, uploadBandwidth, transport;
  139. if (this.bandwidth === null) {
  140. downloadBandwidth = "N/A";
  141. uploadBandwidth = "N/A";
  142. } else {
  143. downloadBandwidth = this.bandwidth.download?
  144. this.bandwidth.download + " Kbps" :
  145. "N/A";
  146. uploadBandwidth = this.bandwidth.upload?
  147. this.bandwidth.upload + " Kbps" :
  148. "N/A";
  149. }
  150. if (!this.transport || this.transport.length === 0) {
  151. transport = "<tr>" +
  152. "<td><span " +
  153. "data-i18n='connectionindicator.address'>" +
  154. translate("connectionindicator.address") + "</span></td>" +
  155. "<td> N/A</td></tr>";
  156. } else {
  157. var data = {remoteIP: [], localIP:[], remotePort:[], localPort:[]};
  158. for(i = 0; i < this.transport.length; i++) {
  159. var ip = ConnectionIndicator.getIP(this.transport[i].ip);
  160. var port = ConnectionIndicator.getPort(this.transport[i].ip);
  161. var localIP =
  162. ConnectionIndicator.getIP(this.transport[i].localip);
  163. var localPort =
  164. ConnectionIndicator.getPort(this.transport[i].localip);
  165. if(data.remoteIP.indexOf(ip) == -1) {
  166. data.remoteIP.push(ip);
  167. }
  168. if(data.remotePort.indexOf(port) == -1) {
  169. data.remotePort.push(port);
  170. }
  171. if(data.localIP.indexOf(localIP) == -1) {
  172. data.localIP.push(localIP);
  173. }
  174. if(data.localPort.indexOf(localPort) == -1) {
  175. data.localPort.push(localPort);
  176. }
  177. }
  178. var local_address_key = "connectionindicator.localaddress";
  179. var remote_address_key = "connectionindicator.remoteaddress";
  180. var localTransport =
  181. "<tr><td><span data-i18n='" +
  182. local_address_key +"' data-i18n-options='" +
  183. JSON.stringify({count: data.localIP.length}) + "'>" +
  184. translate(local_address_key, {count: data.localIP.length}) +
  185. "</span></td><td> " +
  186. ConnectionIndicator.getStringFromArray(data.localIP) +
  187. "</td></tr>";
  188. transport =
  189. "<tr><td><span data-i18n='" +
  190. remote_address_key + "' data-i18n-options='" +
  191. JSON.stringify({count: data.remoteIP.length}) + "'>" +
  192. translate(remote_address_key,
  193. {count: data.remoteIP.length}) +
  194. "</span></td><td> " +
  195. ConnectionIndicator.getStringFromArray(data.remoteIP) +
  196. "</td></tr>";
  197. var key_remote = "connectionindicator.remoteport",
  198. key_local = "connectionindicator.localport";
  199. transport += "<tr>" +
  200. "<td>" +
  201. "<span data-i18n='" + key_remote +
  202. "' data-i18n-options='" +
  203. JSON.stringify({count: this.transport.length}) + "'>" +
  204. translate(key_remote, {count: this.transport.length}) +
  205. "</span></td><td>";
  206. localTransport += "<tr>" +
  207. "<td>" +
  208. "<span data-i18n='" + key_local +
  209. "' data-i18n-options='" +
  210. JSON.stringify({count: this.transport.length}) + "'>" +
  211. translate(key_local, {count: this.transport.length}) +
  212. "</span></td><td>";
  213. transport +=
  214. ConnectionIndicator.getStringFromArray(data.remotePort);
  215. localTransport +=
  216. ConnectionIndicator.getStringFromArray(data.localPort);
  217. transport += "</td></tr>";
  218. transport += localTransport + "</td></tr>";
  219. transport +="<tr>" +
  220. "<td><span data-i18n='connectionindicator.transport'>" +
  221. translate("connectionindicator.transport") + "</span></td>" +
  222. "<td>" + this.transport[0].type + "</td></tr>";
  223. }
  224. result += "<table style='width:100%'>" +
  225. "<tr>" +
  226. "<td>" +
  227. "<span data-i18n='connectionindicator.bandwidth'>" +
  228. translate("connectionindicator.bandwidth") + "</span>" +
  229. "</td><td>" +
  230. "<span class='jitsipopover__green'>&darr;</span>" +
  231. downloadBandwidth +
  232. " <span class='jitsipopover__orange'>&uarr;</span>" +
  233. uploadBandwidth + "</td></tr>";
  234. result += transport + "</table>";
  235. }
  236. return result;
  237. };
  238. /**
  239. * Shows or hide the additional information.
  240. */
  241. ConnectionIndicator.prototype.showMore = function () {
  242. this.showMoreValue = !this.showMoreValue;
  243. this.updatePopoverData();
  244. };
  245. function createIcon(classes, iconClass) {
  246. var icon = document.createElement("span");
  247. for(var i in classes) {
  248. icon.classList.add(classes[i]);
  249. }
  250. icon.appendChild(
  251. document.createElement("i")).classList.add(iconClass);
  252. return icon;
  253. }
  254. /**
  255. * Creates the indicator
  256. */
  257. ConnectionIndicator.prototype.create = function () {
  258. this.connectionIndicatorContainer = document.createElement("div");
  259. this.connectionIndicatorContainer.className = "connectionindicator";
  260. this.connectionIndicatorContainer.style.display = "none";
  261. this.videoContainer.container.appendChild(
  262. this.connectionIndicatorContainer);
  263. this.popover = new JitsiPopover(
  264. $("#" + this.videoContainer.videoSpanId + " > .connectionindicator"),
  265. {content: "<div class=\"connection_info\" data-i18n='connectionindicator.na'>" +
  266. APP.translation.translateString("connectionindicator.na") + "</div>",
  267. skin: "black"});
  268. // override popover show method to make sure we will update the content
  269. // before showing the popover
  270. var origShowFunc = this.popover.show;
  271. this.popover.show = function () {
  272. // update content by forcing it, to finish even if popover
  273. // is not visible
  274. this.updatePopoverData(true);
  275. // call the original show, passing its actual this
  276. origShowFunc.call(this.popover);
  277. }.bind(this);
  278. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  279. createIcon(["connection", "connection_empty"], "icon-connection"));
  280. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  281. createIcon(["connection", "connection_full"], "icon-connection"));
  282. this.interruptedIndicator = this.connectionIndicatorContainer.appendChild(
  283. createIcon(["connection", "connection_lost"],"icon-connection-lost"));
  284. $(this.interruptedIndicator).hide();
  285. };
  286. /**
  287. * Removes the indicator
  288. */
  289. ConnectionIndicator.prototype.remove = function() {
  290. if (this.connectionIndicatorContainer.parentNode) {
  291. this.connectionIndicatorContainer.parentNode.removeChild(
  292. this.connectionIndicatorContainer);
  293. }
  294. this.popover.forceHide();
  295. };
  296. /**
  297. * Updates the UI which displays warning about user's connectivity problems.
  298. *
  299. * @param {boolean} isActive true if the connection is working fine or false if
  300. * the user is having connectivity issues.
  301. */
  302. ConnectionIndicator.prototype.updateConnectionStatusIndicator
  303. = function (isActive) {
  304. this.isConnectionActive = isActive;
  305. if (this.isConnectionActive) {
  306. $(this.interruptedIndicator).hide();
  307. $(this.emptyIcon).show();
  308. $(this.fullIcon).show();
  309. } else {
  310. $(this.interruptedIndicator).show();
  311. $(this.emptyIcon).hide();
  312. $(this.fullIcon).hide();
  313. this.updateConnectionQuality(0 /* zero bars */);
  314. }
  315. };
  316. /**
  317. * Updates the data of the indicator
  318. * @param percent the percent of connection quality
  319. * @param object the statistics data.
  320. */
  321. ConnectionIndicator.prototype.updateConnectionQuality =
  322. function (percent, object) {
  323. if (percent === null) {
  324. this.connectionIndicatorContainer.style.display = "none";
  325. this.popover.forceHide();
  326. return;
  327. } else {
  328. if(this.connectionIndicatorContainer.style.display == "none") {
  329. this.connectionIndicatorContainer.style.display = "block";
  330. }
  331. }
  332. if (object) {
  333. this.bandwidth = object.bandwidth;
  334. this.bitrate = object.bitrate;
  335. this.packetLoss = object.packetLoss;
  336. this.transport = object.transport;
  337. if (object.resolution) {
  338. this.resolution = object.resolution;
  339. }
  340. }
  341. for (var quality in ConnectionIndicator.connectionQualityValues) {
  342. if (percent >= quality) {
  343. this.fullIcon.style.width =
  344. ConnectionIndicator.connectionQualityValues[quality];
  345. }
  346. }
  347. if (object && typeof object.isResolutionHD === 'boolean') {
  348. this.isResolutionHD = object.isResolutionHD;
  349. }
  350. this.updateResolutionIndicator();
  351. this.updatePopoverData();
  352. };
  353. /**
  354. * Updates the resolution
  355. * @param resolution the new resolution
  356. */
  357. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  358. this.resolution = resolution;
  359. this.updateResolutionIndicator();
  360. this.updatePopoverData();
  361. };
  362. /**
  363. * Updates the content of the popover if its visible
  364. * @param force to work even if popover is not visible
  365. */
  366. ConnectionIndicator.prototype.updatePopoverData = function (force) {
  367. // generate content, translate it and add it to document only if
  368. // popover is visible or we force to do so.
  369. if(this.popover.popoverShown || force) {
  370. this.popover.updateContent(
  371. `<div class="connection_info">${this.generateText()}</div>`
  372. );
  373. APP.translation.translateElement($(".connection_info"));
  374. }
  375. };
  376. /**
  377. * Hides the popover
  378. */
  379. ConnectionIndicator.prototype.hide = function () {
  380. this.popover.forceHide();
  381. };
  382. /**
  383. * Hides the indicator
  384. */
  385. ConnectionIndicator.prototype.hideIndicator = function () {
  386. this.connectionIndicatorContainer.style.display = "none";
  387. if(this.popover)
  388. this.popover.forceHide();
  389. };
  390. /**
  391. * Updates the resolution indicator.
  392. */
  393. ConnectionIndicator.prototype.updateResolutionIndicator = function () {
  394. if (this.id !== null
  395. && VideoLayout.isCurrentlyOnLarge(this.id)) {
  396. let showResolutionLabel = false;
  397. if (this.isResolutionHD !== null)
  398. showResolutionLabel = this.isResolutionHD;
  399. else if (this.resolution !== null) {
  400. let resolutions = this.resolution || {};
  401. Object.keys(resolutions).map(function (ssrc) {
  402. const { height } = resolutions[ssrc];
  403. if (height >= config.minHDHeight)
  404. showResolutionLabel = true;
  405. });
  406. }
  407. VideoLayout.updateResolutionLabel(showResolutionLabel);
  408. }
  409. };
  410. export default ConnectionIndicator;