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

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