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

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