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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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) {
  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("icon-connection");
  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"]));
  254. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  255. createIcon(["connection", "connection_full"]));
  256. };
  257. /**
  258. * Removes the indicator
  259. */
  260. ConnectionIndicator.prototype.remove = function() {
  261. if (this.connectionIndicatorContainer.parentNode) {
  262. this.connectionIndicatorContainer.parentNode.removeChild(
  263. this.connectionIndicatorContainer);
  264. }
  265. this.popover.forceHide();
  266. };
  267. /**
  268. * Updates the data of the indicator
  269. * @param percent the percent of connection quality
  270. * @param object the statistics data.
  271. */
  272. ConnectionIndicator.prototype.updateConnectionQuality =
  273. function (percent, object) {
  274. if (percent === null) {
  275. this.connectionIndicatorContainer.style.display = "none";
  276. this.popover.forceHide();
  277. return;
  278. } else {
  279. if(this.connectionIndicatorContainer.style.display == "none") {
  280. this.connectionIndicatorContainer.style.display = "block";
  281. this.videoContainer.updateIconPositions();
  282. }
  283. }
  284. this.bandwidth = object.bandwidth;
  285. this.bitrate = object.bitrate;
  286. this.packetLoss = object.packetLoss;
  287. this.transport = object.transport;
  288. if (object.resolution) {
  289. this.resolution = object.resolution;
  290. }
  291. for (var quality in ConnectionIndicator.connectionQualityValues) {
  292. if (percent >= quality) {
  293. this.fullIcon.style.width =
  294. ConnectionIndicator.connectionQualityValues[quality];
  295. }
  296. }
  297. if (object.isResolutionHD) {
  298. this.isResolutionHD = object.isResolutionHD;
  299. }
  300. this.updateResolutionIndicator();
  301. this.updatePopoverData();
  302. };
  303. /**
  304. * Updates the resolution
  305. * @param resolution the new resolution
  306. */
  307. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  308. this.resolution = resolution;
  309. this.updateResolutionIndicator();
  310. this.updatePopoverData();
  311. };
  312. /**
  313. * Updates the content of the popover if its visible
  314. * @param force to work even if popover is not visible
  315. */
  316. ConnectionIndicator.prototype.updatePopoverData = function (force) {
  317. // generate content, translate it and add it to document only if
  318. // popover is visible or we force to do so.
  319. if(this.popover.popoverShown || force) {
  320. this.popover.updateContent(
  321. `<div class="connection_info">${this.generateText()}</div>`
  322. );
  323. APP.translation.translateElement($(".connection_info"));
  324. }
  325. };
  326. /**
  327. * Hides the popover
  328. */
  329. ConnectionIndicator.prototype.hide = function () {
  330. this.popover.forceHide();
  331. };
  332. /**
  333. * Hides the indicator
  334. */
  335. ConnectionIndicator.prototype.hideIndicator = function () {
  336. this.connectionIndicatorContainer.style.display = "none";
  337. if(this.popover)
  338. this.popover.forceHide();
  339. };
  340. /**
  341. * Updates the resolution indicator.
  342. */
  343. ConnectionIndicator.prototype.updateResolutionIndicator = function () {
  344. if (this.id !== null
  345. && VideoLayout.isCurrentlyOnLarge(this.id)) {
  346. let showResolutionLabel = false;
  347. if (this.isResolutionHD !== null)
  348. showResolutionLabel = this.isResolutionHD;
  349. else if (this.resolution !== null) {
  350. let resolutions = this.resolution || {};
  351. Object.keys(resolutions).map(function (ssrc) {
  352. let {width, height} = resolutions[ssrc];
  353. if (height >= config.minHDHeight)
  354. showResolutionLabel = true;
  355. });
  356. }
  357. VideoLayout.updateResolutionLabel(showResolutionLabel);
  358. }
  359. };
  360. export default ConnectionIndicator;