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

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