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

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