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

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