Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ConnectionIndicator.js 15KB

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