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

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