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

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