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

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