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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* global APP, $ */
  2. /* jshint -W101 */
  3. import JitsiPopover from "../util/JitsiPopover";
  4. /**
  5. * Constructs new connection indicator.
  6. * @param videoContainer the video container associated with the indicator.
  7. * @constructor
  8. */
  9. function ConnectionIndicator(videoContainer, id) {
  10. this.videoContainer = videoContainer;
  11. this.bandwidth = null;
  12. this.packetLoss = null;
  13. this.bitrate = null;
  14. this.showMoreValue = false;
  15. this.resolution = null;
  16. this.transport = [];
  17. this.popover = null;
  18. this.id = id;
  19. this.create();
  20. }
  21. /**
  22. * Values for the connection quality
  23. * @type {{98: string,
  24. * 81: string,
  25. * 64: string,
  26. * 47: string,
  27. * 30: string,
  28. * 0: string}}
  29. */
  30. ConnectionIndicator.connectionQualityValues = {
  31. 98: "18px", //full
  32. 81: "15px",//4 bars
  33. 64: "11px",//3 bars
  34. 47: "7px",//2 bars
  35. 30: "3px",//1 bar
  36. 0: "0px"//empty
  37. };
  38. ConnectionIndicator.getIP = function(value) {
  39. return value.substring(0, value.lastIndexOf(":"));
  40. };
  41. ConnectionIndicator.getPort = function(value) {
  42. return value.substring(value.lastIndexOf(":") + 1, value.length);
  43. };
  44. ConnectionIndicator.getStringFromArray = function (array) {
  45. var res = "";
  46. for(var i = 0; i < array.length; i++) {
  47. res += (i === 0? "" : ", ") + array[i];
  48. }
  49. return res;
  50. };
  51. /**
  52. * Generates the html content.
  53. * @returns {string} the html content.
  54. */
  55. ConnectionIndicator.prototype.generateText = function () {
  56. var downloadBitrate, uploadBitrate, packetLoss, resolution, i;
  57. var translate = APP.translation.translateString;
  58. if(this.bitrate === null) {
  59. downloadBitrate = "N/A";
  60. uploadBitrate = "N/A";
  61. }
  62. else {
  63. downloadBitrate =
  64. this.bitrate.download? this.bitrate.download + " Kbps" : "N/A";
  65. uploadBitrate =
  66. this.bitrate.upload? this.bitrate.upload + " Kbps" : "N/A";
  67. }
  68. if(this.packetLoss === null) {
  69. packetLoss = "N/A";
  70. } else {
  71. packetLoss = "<span class='jitsipopover_green'>&darr;</span>" +
  72. (this.packetLoss.download !== null ?
  73. this.packetLoss.download : "N/A") +
  74. "% <span class='jitsipopover_orange'>&uarr;</span>" +
  75. (this.packetLoss.upload !== null? this.packetLoss.upload : "N/A") +
  76. "%";
  77. }
  78. var resolutionValue = null;
  79. if(this.resolution && this.id) {
  80. var keys = Object.keys(this.resolution);
  81. for(var ssrc in this.resolution) {
  82. // skip resolutions for ssrc that don't have this info
  83. // like receive-only ssrc for FF
  84. if(this.resolution[ssrc]
  85. && this.resolution[ssrc].height != -1
  86. && this.resolution[ssrc].width != -1)
  87. resolutionValue = this.resolution[ssrc];
  88. }
  89. }
  90. if(this.id === null) {
  91. resolution = "";
  92. if(this.resolution === null || !Object.keys(this.resolution) ||
  93. Object.keys(this.resolution).length === 0) {
  94. resolution = "N/A";
  95. } else {
  96. for (i in this.resolution) {
  97. resolutionValue = this.resolution[i];
  98. if (resolutionValue) {
  99. if (resolutionValue.height &&
  100. resolutionValue.width) {
  101. resolution += (resolution === "" ? "" : ", ") +
  102. resolutionValue.width + "x" +
  103. resolutionValue.height;
  104. }
  105. }
  106. }
  107. }
  108. } else if(!resolutionValue ||
  109. !resolutionValue.height ||
  110. !resolutionValue.width) {
  111. resolution = "N/A";
  112. } else {
  113. resolution = resolutionValue.width + "x" + resolutionValue.height;
  114. }
  115. var result = "<table style='width:100%'>" +
  116. "<tr>" +
  117. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.bitrate'>" +
  118. translate("connectionindicator.bitrate") + "</span></td>" +
  119. "<td><span class='jitsipopover_green'>&darr;</span>" +
  120. downloadBitrate + " <span class='jitsipopover_orange'>&uarr;</span>" +
  121. uploadBitrate + "</td>" +
  122. "</tr><tr>" +
  123. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.packetloss'>" +
  124. translate("connectionindicator.packetloss") + "</span></td>" +
  125. "<td>" + packetLoss + "</td>" +
  126. "</tr><tr>" +
  127. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.resolution'>" +
  128. translate("connectionindicator.resolution") + "</span></td>" +
  129. "<td>" + resolution + "</td></tr></table>";
  130. if(this.videoContainer.videoSpanId == "localVideoContainer") {
  131. result += "<div class=\"jitsipopover_showmore\" " +
  132. "onclick = \"APP.UI.connectionIndicatorShowMore('" +
  133. // FIXME: we do not know local id when this text is generated
  134. //this.id + "')\" data-i18n='connectionindicator." +
  135. "local')\" data-i18n='connectionindicator." +
  136. (this.showMoreValue ? "less" : "more") + "'>" +
  137. translate("connectionindicator." + (this.showMoreValue ? "less" : "more")) +
  138. "</div><br />";
  139. }
  140. if (this.showMoreValue) {
  141. var downloadBandwidth, uploadBandwidth, transport;
  142. if (this.bandwidth === null) {
  143. downloadBandwidth = "N/A";
  144. uploadBandwidth = "N/A";
  145. } else {
  146. downloadBandwidth = this.bandwidth.download?
  147. this.bandwidth.download + " Kbps" :
  148. "N/A";
  149. uploadBandwidth = this.bandwidth.upload?
  150. this.bandwidth.upload + " Kbps" :
  151. "N/A";
  152. }
  153. if (!this.transport || this.transport.length === 0) {
  154. transport = "<tr>" +
  155. "<td><span class='jitsipopover_blue' " +
  156. "data-i18n='connectionindicator.address'>" +
  157. translate("connectionindicator.address") + "</span></td>" +
  158. "<td> N/A</td></tr>";
  159. } else {
  160. var data = {remoteIP: [], localIP:[], remotePort:[], localPort:[]};
  161. for(i = 0; i < this.transport.length; i++) {
  162. var ip = ConnectionIndicator.getIP(this.transport[i].ip);
  163. var port = ConnectionIndicator.getPort(this.transport[i].ip);
  164. var localIP =
  165. ConnectionIndicator.getIP(this.transport[i].localip);
  166. var localPort =
  167. ConnectionIndicator.getPort(this.transport[i].localip);
  168. if(data.remoteIP.indexOf(ip) == -1) {
  169. data.remoteIP.push(ip);
  170. }
  171. if(data.remotePort.indexOf(port) == -1) {
  172. data.remotePort.push(port);
  173. }
  174. if(data.localIP.indexOf(localIP) == -1) {
  175. data.localIP.push(localIP);
  176. }
  177. if(data.localPort.indexOf(localPort) == -1) {
  178. data.localPort.push(localPort);
  179. }
  180. }
  181. var local_address_key = "connectionindicator.localaddress";
  182. var remote_address_key = "connectionindicator.remoteaddress";
  183. var localTransport =
  184. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  185. local_address_key +"' data-i18n-options='" +
  186. JSON.stringify({count: data.localIP.length}) + "'>" +
  187. translate(local_address_key, {count: data.localIP.length}) +
  188. "</span></td><td> " +
  189. ConnectionIndicator.getStringFromArray(data.localIP) +
  190. "</td></tr>";
  191. transport =
  192. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  193. remote_address_key + "' data-i18n-options='" +
  194. JSON.stringify({count: data.remoteIP.length}) + "'>" +
  195. translate(remote_address_key,
  196. {count: data.remoteIP.length}) +
  197. "</span></td><td> " +
  198. ConnectionIndicator.getStringFromArray(data.remoteIP) +
  199. "</td></tr>";
  200. var key_remote = "connectionindicator.remoteport",
  201. key_local = "connectionindicator.localport";
  202. transport += "<tr>" +
  203. "<td>" +
  204. "<span class='jitsipopover_blue' data-i18n='" + key_remote +
  205. "' data-i18n-options='" +
  206. JSON.stringify({count: this.transport.length}) + "'>" +
  207. translate(key_remote, {count: this.transport.length}) +
  208. "</span></td><td>";
  209. localTransport += "<tr>" +
  210. "<td>" +
  211. "<span class='jitsipopover_blue' data-i18n='" + key_local +
  212. "' data-i18n-options='" +
  213. JSON.stringify({count: this.transport.length}) + "'>" +
  214. translate(key_local, {count: this.transport.length}) +
  215. "</span></td><td>";
  216. transport +=
  217. ConnectionIndicator.getStringFromArray(data.remotePort);
  218. localTransport +=
  219. ConnectionIndicator.getStringFromArray(data.localPort);
  220. transport += "</td></tr>";
  221. transport += localTransport + "</td></tr>";
  222. transport +="<tr>" +
  223. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.transport'>" +
  224. translate("connectionindicator.transport") + "</span></td>" +
  225. "<td>" + this.transport[0].type + "</td></tr>";
  226. }
  227. result += "<table style='width:100%'>" +
  228. "<tr>" +
  229. "<td>" +
  230. "<span class='jitsipopover_blue' data-i18n='connectionindicator.bandwidth'>" +
  231. translate("connectionindicator.bandwidth") + "</span>" +
  232. "</td><td>" +
  233. "<span class='jitsipopover_green'>&darr;</span>" +
  234. downloadBandwidth +
  235. " <span class='jitsipopover_orange'>&uarr;</span>" +
  236. uploadBandwidth + "</td></tr>";
  237. result += transport + "</table>";
  238. }
  239. return result;
  240. };
  241. /**
  242. * Shows or hide the additional information.
  243. */
  244. ConnectionIndicator.prototype.showMore = function () {
  245. this.showMoreValue = !this.showMoreValue;
  246. this.updatePopoverData();
  247. };
  248. function createIcon(classes) {
  249. var icon = document.createElement("span");
  250. for(var i in classes) {
  251. icon.classList.add(classes[i]);
  252. }
  253. icon.appendChild(
  254. document.createElement("i")).classList.add("icon-connection");
  255. return icon;
  256. }
  257. /**
  258. * Creates the indicator
  259. */
  260. ConnectionIndicator.prototype.create = function () {
  261. this.connectionIndicatorContainer = document.createElement("div");
  262. this.connectionIndicatorContainer.className = "connectionindicator";
  263. this.connectionIndicatorContainer.style.display = "none";
  264. this.videoContainer.container.appendChild(
  265. this.connectionIndicatorContainer);
  266. this.popover = new JitsiPopover(
  267. $("#" + this.videoContainer.videoSpanId + " > .connectionindicator"),
  268. {content: "<div class=\"connection_info\" data-i18n='connectionindicator.na'>" +
  269. APP.translation.translateString("connectionindicator.na") + "</div>",
  270. skin: "black"});
  271. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  272. createIcon(["connection", "connection_empty"]));
  273. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  274. createIcon(["connection", "connection_full"]));
  275. };
  276. /**
  277. * Removes the indicator
  278. */
  279. ConnectionIndicator.prototype.remove = function() {
  280. if (this.connectionIndicatorContainer.parentNode) {
  281. this.connectionIndicatorContainer.parentNode.removeChild(
  282. this.connectionIndicatorContainer);
  283. }
  284. this.popover.forceHide();
  285. };
  286. /**
  287. * Updates the data of the indicator
  288. * @param percent the percent of connection quality
  289. * @param object the statistics data.
  290. */
  291. ConnectionIndicator.prototype.updateConnectionQuality =
  292. function (percent, object) {
  293. if (percent === null) {
  294. this.connectionIndicatorContainer.style.display = "none";
  295. this.popover.forceHide();
  296. return;
  297. } else {
  298. if(this.connectionIndicatorContainer.style.display == "none") {
  299. this.connectionIndicatorContainer.style.display = "block";
  300. this.videoContainer.updateIconPositions();
  301. }
  302. }
  303. this.bandwidth = object.bandwidth;
  304. this.bitrate = object.bitrate;
  305. this.packetLoss = object.packetLoss;
  306. this.transport = object.transport;
  307. if (object.resolution) {
  308. this.resolution = object.resolution;
  309. }
  310. for (var quality in ConnectionIndicator.connectionQualityValues) {
  311. if (percent >= quality) {
  312. this.fullIcon.style.width =
  313. ConnectionIndicator.connectionQualityValues[quality];
  314. }
  315. }
  316. this.updatePopoverData();
  317. };
  318. /**
  319. * Updates the resolution
  320. * @param resolution the new resolution
  321. */
  322. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  323. this.resolution = resolution;
  324. this.updatePopoverData();
  325. };
  326. /**
  327. * Updates the content of the popover
  328. */
  329. ConnectionIndicator.prototype.updatePopoverData = function () {
  330. this.popover.updateContent(
  331. `<div class="connection_info">${this.generateText()}</div>`
  332. );
  333. APP.translation.translateElement($(".connection_info"));
  334. };
  335. /**
  336. * Hides the popover
  337. */
  338. ConnectionIndicator.prototype.hide = function () {
  339. this.popover.forceHide();
  340. };
  341. /**
  342. * Hides the indicator
  343. */
  344. ConnectionIndicator.prototype.hideIndicator = function () {
  345. this.connectionIndicatorContainer.style.display = "none";
  346. if(this.popover)
  347. this.popover.forceHide();
  348. };
  349. export default ConnectionIndicator;