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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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, 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. // GENERATE RESOLUTIONS STRING
  79. let resolutions = this.resolution || {};
  80. let resolutionStr = Object.keys(resolutions).map(function (ssrc) {
  81. let {width, height} = resolutions[ssrc];
  82. return `${width}x${height}`;
  83. }).join(', ') || 'N/A';
  84. var result = "<table style='width:100%'>" +
  85. "<tr>" +
  86. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.bitrate'>" +
  87. translate("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'>" +
  93. translate("connectionindicator.packetloss") + "</span></td>" +
  94. "<td>" + packetLoss + "</td>" +
  95. "</tr><tr>" +
  96. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.resolution'>" +
  97. translate("connectionindicator.resolution") + "</span></td>" +
  98. "<td>" + resolutionStr + "</td></tr></table>";
  99. if(this.videoContainer.videoSpanId == "localVideoContainer") {
  100. result += "<div class=\"jitsipopover_showmore\" " +
  101. "onclick = \"APP.UI.connectionIndicatorShowMore('" +
  102. // FIXME: we do not know local id when this text is generated
  103. //this.id + "')\" data-i18n='connectionindicator." +
  104. "local')\" data-i18n='connectionindicator." +
  105. (this.showMoreValue ? "less" : "more") + "'>" +
  106. translate("connectionindicator." + (this.showMoreValue ? "less" : "more")) +
  107. "</div><br />";
  108. }
  109. if (this.showMoreValue) {
  110. var downloadBandwidth, uploadBandwidth, transport;
  111. if (this.bandwidth === null) {
  112. downloadBandwidth = "N/A";
  113. uploadBandwidth = "N/A";
  114. } else {
  115. downloadBandwidth = this.bandwidth.download?
  116. this.bandwidth.download + " Kbps" :
  117. "N/A";
  118. uploadBandwidth = this.bandwidth.upload?
  119. this.bandwidth.upload + " Kbps" :
  120. "N/A";
  121. }
  122. if (!this.transport || this.transport.length === 0) {
  123. transport = "<tr>" +
  124. "<td><span class='jitsipopover_blue' " +
  125. "data-i18n='connectionindicator.address'>" +
  126. translate("connectionindicator.address") + "</span></td>" +
  127. "<td> N/A</td></tr>";
  128. } else {
  129. var data = {remoteIP: [], localIP:[], remotePort:[], localPort:[]};
  130. for(i = 0; i < this.transport.length; i++) {
  131. var ip = ConnectionIndicator.getIP(this.transport[i].ip);
  132. var port = ConnectionIndicator.getPort(this.transport[i].ip);
  133. var localIP =
  134. ConnectionIndicator.getIP(this.transport[i].localip);
  135. var localPort =
  136. ConnectionIndicator.getPort(this.transport[i].localip);
  137. if(data.remoteIP.indexOf(ip) == -1) {
  138. data.remoteIP.push(ip);
  139. }
  140. if(data.remotePort.indexOf(port) == -1) {
  141. data.remotePort.push(port);
  142. }
  143. if(data.localIP.indexOf(localIP) == -1) {
  144. data.localIP.push(localIP);
  145. }
  146. if(data.localPort.indexOf(localPort) == -1) {
  147. data.localPort.push(localPort);
  148. }
  149. }
  150. var local_address_key = "connectionindicator.localaddress";
  151. var remote_address_key = "connectionindicator.remoteaddress";
  152. var localTransport =
  153. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  154. local_address_key +"' data-i18n-options='" +
  155. JSON.stringify({count: data.localIP.length}) + "'>" +
  156. translate(local_address_key, {count: data.localIP.length}) +
  157. "</span></td><td> " +
  158. ConnectionIndicator.getStringFromArray(data.localIP) +
  159. "</td></tr>";
  160. transport =
  161. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  162. remote_address_key + "' data-i18n-options='" +
  163. JSON.stringify({count: data.remoteIP.length}) + "'>" +
  164. translate(remote_address_key,
  165. {count: data.remoteIP.length}) +
  166. "</span></td><td> " +
  167. ConnectionIndicator.getStringFromArray(data.remoteIP) +
  168. "</td></tr>";
  169. var key_remote = "connectionindicator.remoteport",
  170. key_local = "connectionindicator.localport";
  171. transport += "<tr>" +
  172. "<td>" +
  173. "<span class='jitsipopover_blue' data-i18n='" + key_remote +
  174. "' data-i18n-options='" +
  175. JSON.stringify({count: this.transport.length}) + "'>" +
  176. translate(key_remote, {count: this.transport.length}) +
  177. "</span></td><td>";
  178. localTransport += "<tr>" +
  179. "<td>" +
  180. "<span class='jitsipopover_blue' data-i18n='" + key_local +
  181. "' data-i18n-options='" +
  182. JSON.stringify({count: this.transport.length}) + "'>" +
  183. translate(key_local, {count: this.transport.length}) +
  184. "</span></td><td>";
  185. transport +=
  186. ConnectionIndicator.getStringFromArray(data.remotePort);
  187. localTransport +=
  188. ConnectionIndicator.getStringFromArray(data.localPort);
  189. transport += "</td></tr>";
  190. transport += localTransport + "</td></tr>";
  191. transport +="<tr>" +
  192. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.transport'>" +
  193. translate("connectionindicator.transport") + "</span></td>" +
  194. "<td>" + this.transport[0].type + "</td></tr>";
  195. }
  196. result += "<table style='width:100%'>" +
  197. "<tr>" +
  198. "<td>" +
  199. "<span class='jitsipopover_blue' data-i18n='connectionindicator.bandwidth'>" +
  200. translate("connectionindicator.bandwidth") + "</span>" +
  201. "</td><td>" +
  202. "<span class='jitsipopover_green'>&darr;</span>" +
  203. downloadBandwidth +
  204. " <span class='jitsipopover_orange'>&uarr;</span>" +
  205. uploadBandwidth + "</td></tr>";
  206. result += transport + "</table>";
  207. }
  208. return result;
  209. };
  210. /**
  211. * Shows or hide the additional information.
  212. */
  213. ConnectionIndicator.prototype.showMore = function () {
  214. this.showMoreValue = !this.showMoreValue;
  215. this.updatePopoverData();
  216. };
  217. function createIcon(classes) {
  218. var icon = document.createElement("span");
  219. for(var i in classes) {
  220. icon.classList.add(classes[i]);
  221. }
  222. icon.appendChild(
  223. document.createElement("i")).classList.add("icon-connection");
  224. return icon;
  225. }
  226. /**
  227. * Creates the indicator
  228. */
  229. ConnectionIndicator.prototype.create = function () {
  230. this.connectionIndicatorContainer = document.createElement("div");
  231. this.connectionIndicatorContainer.className = "connectionindicator";
  232. this.connectionIndicatorContainer.style.display = "none";
  233. this.videoContainer.container.appendChild(
  234. this.connectionIndicatorContainer);
  235. this.popover = new JitsiPopover(
  236. $("#" + this.videoContainer.videoSpanId + " > .connectionindicator"),
  237. {content: "<div class=\"connection_info\" data-i18n='connectionindicator.na'>" +
  238. APP.translation.translateString("connectionindicator.na") + "</div>",
  239. skin: "black"});
  240. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  241. createIcon(["connection", "connection_empty"]));
  242. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  243. createIcon(["connection", "connection_full"]));
  244. };
  245. /**
  246. * Removes the indicator
  247. */
  248. ConnectionIndicator.prototype.remove = function() {
  249. if (this.connectionIndicatorContainer.parentNode) {
  250. this.connectionIndicatorContainer.parentNode.removeChild(
  251. this.connectionIndicatorContainer);
  252. }
  253. this.popover.forceHide();
  254. };
  255. /**
  256. * Updates the data of the indicator
  257. * @param percent the percent of connection quality
  258. * @param object the statistics data.
  259. */
  260. ConnectionIndicator.prototype.updateConnectionQuality =
  261. function (percent, object) {
  262. if (percent === null) {
  263. this.connectionIndicatorContainer.style.display = "none";
  264. this.popover.forceHide();
  265. return;
  266. } else {
  267. if(this.connectionIndicatorContainer.style.display == "none") {
  268. this.connectionIndicatorContainer.style.display = "block";
  269. this.videoContainer.updateIconPositions();
  270. }
  271. }
  272. this.bandwidth = object.bandwidth;
  273. this.bitrate = object.bitrate;
  274. this.packetLoss = object.packetLoss;
  275. this.transport = object.transport;
  276. if (object.resolution) {
  277. this.resolution = object.resolution;
  278. }
  279. for (var quality in ConnectionIndicator.connectionQualityValues) {
  280. if (percent >= quality) {
  281. this.fullIcon.style.width =
  282. ConnectionIndicator.connectionQualityValues[quality];
  283. }
  284. }
  285. this.updatePopoverData();
  286. };
  287. /**
  288. * Updates the resolution
  289. * @param resolution the new resolution
  290. */
  291. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  292. this.resolution = resolution;
  293. this.updatePopoverData();
  294. };
  295. /**
  296. * Updates the content of the popover
  297. */
  298. ConnectionIndicator.prototype.updatePopoverData = function () {
  299. this.popover.updateContent(
  300. `<div class="connection_info">${this.generateText()}</div>`
  301. );
  302. APP.translation.translateElement($(".connection_info"));
  303. };
  304. /**
  305. * Hides the popover
  306. */
  307. ConnectionIndicator.prototype.hide = function () {
  308. this.popover.forceHide();
  309. };
  310. /**
  311. * Hides the indicator
  312. */
  313. ConnectionIndicator.prototype.hideIndicator = function () {
  314. this.connectionIndicatorContainer.style.display = "none";
  315. if(this.popover)
  316. this.popover.forceHide();
  317. };
  318. export default ConnectionIndicator;