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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. this.connectionIndicatorContainer = element;
  242. let popoverContent = (
  243. `<div class="connection-info" data-i18n="${indicatorId}.na"></div>`
  244. );
  245. this.popover = new JitsiPopover($(element), {
  246. content: popoverContent,
  247. skin: "black",
  248. onBeforePosition: el => APP.translation.translateElement(el)
  249. });
  250. // override popover show method to make sure we will update the content
  251. // before showing the popover
  252. var origShowFunc = this.popover.show;
  253. this.popover.show = function () {
  254. // update content by forcing it, to finish even if popover
  255. // is not visible
  256. this.updatePopoverData(true);
  257. // call the original show, passing its actual this
  258. origShowFunc.call(this.popover);
  259. }.bind(this);
  260. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  261. createIcon(["connection", "connection_empty"], "icon-connection"));
  262. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  263. createIcon(["connection", "connection_full"], "icon-connection"));
  264. this.interruptedIndicator = this.connectionIndicatorContainer.appendChild(
  265. createIcon(["connection", "connection_lost"],"icon-connection-lost"));
  266. $(this.interruptedIndicator).hide();
  267. };
  268. /**
  269. * Removes the indicator
  270. */
  271. ConnectionIndicator.prototype.remove = function() {
  272. if (this.connectionIndicatorContainer.parentNode) {
  273. this.connectionIndicatorContainer.parentNode.removeChild(
  274. this.connectionIndicatorContainer);
  275. }
  276. this.popover.forceHide();
  277. };
  278. /**
  279. * Updates the UI which displays warning about user's connectivity problems.
  280. *
  281. * @param {boolean} isActive true if the connection is working fine or false if
  282. * the user is having connectivity issues.
  283. */
  284. ConnectionIndicator.prototype.updateConnectionStatusIndicator
  285. = function (isActive) {
  286. this.isConnectionActive = isActive;
  287. if (this.isConnectionActive) {
  288. $(this.interruptedIndicator).hide();
  289. $(this.emptyIcon).show();
  290. $(this.fullIcon).show();
  291. } else {
  292. $(this.interruptedIndicator).show();
  293. $(this.emptyIcon).hide();
  294. $(this.fullIcon).hide();
  295. this.updateConnectionQuality(0 /* zero bars */);
  296. }
  297. };
  298. /**
  299. * Updates the data of the indicator
  300. * @param percent the percent of connection quality
  301. * @param object the statistics data.
  302. */
  303. ConnectionIndicator.prototype.updateConnectionQuality =
  304. function (percent, object) {
  305. if (percent === null) {
  306. this.connectionIndicatorContainer.style.display = "none";
  307. this.popover.forceHide();
  308. return;
  309. } else {
  310. if(this.connectionIndicatorContainer.style.display == "none") {
  311. this.connectionIndicatorContainer.style.display = "block";
  312. }
  313. }
  314. if (object) {
  315. this.bandwidth = object.bandwidth;
  316. this.bitrate = object.bitrate;
  317. this.packetLoss = object.packetLoss;
  318. this.transport = object.transport;
  319. if (object.resolution) {
  320. this.resolution = object.resolution;
  321. }
  322. }
  323. for (var quality in ConnectionIndicator.connectionQualityValues) {
  324. if (percent >= quality) {
  325. this.fullIcon.style.width =
  326. ConnectionIndicator.connectionQualityValues[quality];
  327. }
  328. }
  329. if (object && typeof object.isResolutionHD === 'boolean') {
  330. this.isResolutionHD = object.isResolutionHD;
  331. }
  332. this.updateResolutionIndicator();
  333. this.updatePopoverData();
  334. };
  335. /**
  336. * Updates the resolution
  337. * @param resolution the new resolution
  338. */
  339. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  340. this.resolution = resolution;
  341. this.updateResolutionIndicator();
  342. this.updatePopoverData();
  343. };
  344. /**
  345. * Updates the content of the popover if its visible
  346. * @param force to work even if popover is not visible
  347. */
  348. ConnectionIndicator.prototype.updatePopoverData = function (force) {
  349. // generate content, translate it and add it to document only if
  350. // popover is visible or we force to do so.
  351. if(this.popover.popoverShown || force) {
  352. this.popover.updateContent(
  353. `<div class="connection-info">${this.generateText()}</div>`
  354. );
  355. }
  356. };
  357. /**
  358. * Hides the popover
  359. */
  360. ConnectionIndicator.prototype.hide = function () {
  361. this.popover.forceHide();
  362. };
  363. /**
  364. * Hides the indicator
  365. */
  366. ConnectionIndicator.prototype.hideIndicator = function () {
  367. this.connectionIndicatorContainer.style.display = "none";
  368. if(this.popover)
  369. this.popover.forceHide();
  370. };
  371. /**
  372. * Updates the resolution indicator.
  373. */
  374. ConnectionIndicator.prototype.updateResolutionIndicator = function () {
  375. if (this.id !== null
  376. && VideoLayout.isCurrentlyOnLarge(this.id)) {
  377. let showResolutionLabel = false;
  378. if (this.isResolutionHD !== null)
  379. showResolutionLabel = this.isResolutionHD;
  380. else if (this.resolution !== null) {
  381. let resolutions = this.resolution || {};
  382. Object.keys(resolutions).map(function (ssrc) {
  383. const { height } = resolutions[ssrc];
  384. if (height >= config.minHDHeight)
  385. showResolutionLabel = true;
  386. });
  387. }
  388. VideoLayout.updateResolutionLabel(showResolutionLabel);
  389. }
  390. };
  391. export default ConnectionIndicator;