您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConnectionIndicator.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* global $, APP, config */
  2. /* jshint -W101 */
  3. import JitsiPopover from "../util/JitsiPopover";
  4. import VideoLayout from "./VideoLayout";
  5. import UIUtil from "../util/UIUtil";
  6. /**
  7. * Maps a connection quality value (in percent) to the width of the "full" icon.
  8. */
  9. const qualityToWidth = [
  10. // Full (5 bars)
  11. {percent: 80, width: "100%"},
  12. // 4 bars
  13. {percent: 60, width: "80%"},
  14. // 3 bars
  15. {percent: 40, width: "55%"},
  16. // 2 bars
  17. {percent: 20, width: "40%"},
  18. // 1 bar
  19. {percent: 0, width: "20%"}
  20. // Note: we never show 0 bars.
  21. ];
  22. /**
  23. * Constructs new connection indicator.
  24. * @param videoContainer the video container associated with the indicator.
  25. * @param videoId the identifier of the video
  26. * @constructor
  27. */
  28. function ConnectionIndicator(videoContainer, videoId) {
  29. this.videoContainer = videoContainer;
  30. this.bandwidth = null;
  31. this.packetLoss = null;
  32. this.bitrate = null;
  33. this.showMoreValue = false;
  34. this.resolution = null;
  35. this.isResolutionHD = null;
  36. this.transport = [];
  37. this.popover = null;
  38. this.id = videoId;
  39. this.create();
  40. }
  41. ConnectionIndicator.getIP = function(value) {
  42. return value.substring(0, value.lastIndexOf(":"));
  43. };
  44. ConnectionIndicator.getPort = function(value) {
  45. return value.substring(value.lastIndexOf(":") + 1, value.length);
  46. };
  47. ConnectionIndicator.getStringFromArray = function (array) {
  48. var res = "";
  49. for(var i = 0; i < array.length; i++) {
  50. res += (i === 0? "" : ", ") + array[i];
  51. }
  52. return res;
  53. };
  54. /**
  55. * Generates the html content.
  56. * @returns {string} the html content.
  57. */
  58. ConnectionIndicator.prototype.generateText = function () {
  59. var downloadBitrate, uploadBitrate, packetLoss, i;
  60. if(!this.bitrate) {
  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) {
  71. packetLoss = "N/A";
  72. } else {
  73. packetLoss = "<span class='connection-info__download'>&darr;</span>" +
  74. (this.packetLoss.download !== null ?
  75. this.packetLoss.download : "N/A") +
  76. "% <span class='connection-info__upload'>&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. let result = (
  87. `<table class="connection-info__container" style='width:100%'>
  88. <tr>
  89. <td>
  90. <span data-i18n='connectionindicator.bitrate'></span>
  91. </td>
  92. <td>
  93. <span class='connection-info__download'>&darr;</span>${downloadBitrate}
  94. <span class='connection-info__upload'>&uarr;</span>${uploadBitrate}
  95. </td>
  96. </tr>
  97. <tr>
  98. <td>
  99. <span data-i18n='connectionindicator.packetloss'></span>
  100. </td>
  101. <td>${packetLoss}</td>
  102. </tr>
  103. <tr>
  104. <td>
  105. <span data-i18n='connectionindicator.resolution'></span>
  106. </td>
  107. <td>
  108. ${resolutionStr}
  109. </td>
  110. </tr>
  111. </table>`);
  112. if(this.videoContainer.videoSpanId == "localVideoContainer") {
  113. result += "<a class=\"jitsipopover__showmore link\" " +
  114. "onclick = \"APP.UI.connectionIndicatorShowMore('" +
  115. // FIXME: we do not know local id when this text is generated
  116. //this.id + "')\" data-i18n='connectionindicator." +
  117. "local')\" data-i18n='connectionindicator." +
  118. (this.showMoreValue ? "less" : "more") + "'></a>";
  119. }
  120. if (this.showMoreValue) {
  121. var downloadBandwidth, uploadBandwidth, transport;
  122. if (!this.bandwidth) {
  123. downloadBandwidth = "N/A";
  124. uploadBandwidth = "N/A";
  125. } else {
  126. downloadBandwidth = this.bandwidth.download?
  127. this.bandwidth.download + " Kbps" :
  128. "N/A";
  129. uploadBandwidth = this.bandwidth.upload?
  130. this.bandwidth.upload + " Kbps" :
  131. "N/A";
  132. }
  133. if (!this.transport || this.transport.length === 0) {
  134. transport = "<tr>" +
  135. "<td><span " +
  136. "data-i18n='connectionindicator.address'></span></td>" +
  137. "<td> N/A</td></tr>";
  138. } else {
  139. var data = {remoteIP: [], localIP:[], remotePort:[], localPort:[]};
  140. for(i = 0; i < this.transport.length; i++) {
  141. var ip = ConnectionIndicator.getIP(this.transport[i].ip);
  142. var port = ConnectionIndicator.getPort(this.transport[i].ip);
  143. var localIP =
  144. ConnectionIndicator.getIP(this.transport[i].localip);
  145. var localPort =
  146. ConnectionIndicator.getPort(this.transport[i].localip);
  147. if(data.remoteIP.indexOf(ip) == -1) {
  148. data.remoteIP.push(ip);
  149. }
  150. if(data.remotePort.indexOf(port) == -1) {
  151. data.remotePort.push(port);
  152. }
  153. if(data.localIP.indexOf(localIP) == -1) {
  154. data.localIP.push(localIP);
  155. }
  156. if(data.localPort.indexOf(localPort) == -1) {
  157. data.localPort.push(localPort);
  158. }
  159. }
  160. var local_address_key = "connectionindicator.localaddress";
  161. var remote_address_key = "connectionindicator.remoteaddress";
  162. var localTransport =
  163. "<tr><td><span data-i18n='" +
  164. local_address_key +"' data-i18n-options='" +
  165. JSON.stringify({count: data.localIP.length})
  166. + "'></span></td><td> " +
  167. ConnectionIndicator.getStringFromArray(data.localIP) +
  168. "</td></tr>";
  169. transport =
  170. "<tr><td><span data-i18n='" +
  171. remote_address_key + "' data-i18n-options='" +
  172. JSON.stringify({count: data.remoteIP.length})
  173. + "'></span></td><td> " +
  174. ConnectionIndicator.getStringFromArray(data.remoteIP) +
  175. "</td></tr>";
  176. var key_remote = "connectionindicator.remoteport",
  177. key_local = "connectionindicator.localport";
  178. transport += "<tr>" +
  179. "<td>" +
  180. "<span data-i18n='" + key_remote +
  181. "' data-i18n-options='" +
  182. JSON.stringify({count: this.transport.length})
  183. + "'></span></td><td>";
  184. localTransport += "<tr>" +
  185. "<td>" +
  186. "<span data-i18n='" + key_local +
  187. "' data-i18n-options='" +
  188. JSON.stringify({count: this.transport.length})
  189. + "'></span></td><td>";
  190. transport +=
  191. ConnectionIndicator.getStringFromArray(data.remotePort);
  192. localTransport +=
  193. ConnectionIndicator.getStringFromArray(data.localPort);
  194. transport += "</td></tr>";
  195. transport += localTransport + "</td></tr>";
  196. transport +="<tr>" +
  197. "<td><span data-i18n='connectionindicator.transport'>" +
  198. "</span></td>" +
  199. "<td>" + this.transport[0].type + "</td></tr>";
  200. }
  201. result += "<table class='connection-info__container' style='width:100%'>" +
  202. "<tr>" +
  203. "<td>" +
  204. "<span data-i18n='connectionindicator.bandwidth'></span>" +
  205. "</td><td>" +
  206. "<span class='connection-info__download'>&darr;</span>" +
  207. downloadBandwidth +
  208. " <span class='connection-info__upload'>&uarr;</span>" +
  209. uploadBandwidth + "</td></tr>";
  210. result += transport + "</table>";
  211. }
  212. return result;
  213. };
  214. /**
  215. * Shows or hide the additional information.
  216. */
  217. ConnectionIndicator.prototype.showMore = function () {
  218. this.showMoreValue = !this.showMoreValue;
  219. this.updatePopoverData();
  220. };
  221. function createIcon(classes, iconClass) {
  222. var icon = document.createElement("span");
  223. for(var i in classes) {
  224. icon.classList.add(classes[i]);
  225. }
  226. icon.appendChild(
  227. document.createElement("i")).classList.add(iconClass);
  228. return icon;
  229. }
  230. /**
  231. * Creates the indicator
  232. */
  233. ConnectionIndicator.prototype.create = function () {
  234. let indicatorId = 'connectionindicator';
  235. let element = UIUtil.getVideoThumbnailIndicatorSpan({
  236. videoSpanId: this.videoContainer.videoSpanId,
  237. indicatorId
  238. });
  239. element.classList.add('show');
  240. this.connectionIndicatorContainer = element;
  241. let popoverContent = (
  242. `<div class="connection-info" data-i18n="${indicatorId}.na"></div>`
  243. );
  244. this.popover = new JitsiPopover($(element), {
  245. content: popoverContent,
  246. skin: "black",
  247. onBeforePosition: el => APP.translation.translateElement(el)
  248. });
  249. // override popover show method to make sure we will update the content
  250. // before showing the popover
  251. var origShowFunc = this.popover.show;
  252. this.popover.show = function () {
  253. // update content by forcing it, to finish even if popover
  254. // is not visible
  255. this.updatePopoverData(true);
  256. // call the original show, passing its actual this
  257. origShowFunc.call(this.popover);
  258. }.bind(this);
  259. let connectionIconContainer = document.createElement('div');
  260. connectionIconContainer.className = 'connection indicatoricon';
  261. this.emptyIcon = connectionIconContainer.appendChild(
  262. createIcon(["connection_empty"], "icon-connection"));
  263. this.fullIcon = connectionIconContainer.appendChild(
  264. createIcon(["connection_full"], "icon-connection"));
  265. this.interruptedIndicator = connectionIconContainer.appendChild(
  266. createIcon(["connection_lost"],"icon-connection-lost"));
  267. $(this.interruptedIndicator).hide();
  268. this.connectionIndicatorContainer.appendChild(connectionIconContainer);
  269. };
  270. /**
  271. * Removes the indicator
  272. */
  273. ConnectionIndicator.prototype.remove = function() {
  274. if (this.connectionIndicatorContainer.parentNode) {
  275. this.connectionIndicatorContainer.parentNode.removeChild(
  276. this.connectionIndicatorContainer);
  277. }
  278. this.popover.forceHide();
  279. };
  280. /**
  281. * Updates the UI which displays warning about user's connectivity problems.
  282. *
  283. * @param {boolean} isActive true if the connection is working fine or false if
  284. * the user is having connectivity issues.
  285. */
  286. ConnectionIndicator.prototype.updateConnectionStatusIndicator
  287. = function (isActive) {
  288. this.isConnectionActive = isActive;
  289. if (this.isConnectionActive) {
  290. $(this.interruptedIndicator).hide();
  291. $(this.emptyIcon).show();
  292. $(this.fullIcon).show();
  293. } else {
  294. $(this.interruptedIndicator).show();
  295. $(this.emptyIcon).hide();
  296. $(this.fullIcon).hide();
  297. }
  298. };
  299. /**
  300. * Updates the data of the indicator
  301. * @param percent the percent of connection quality
  302. * @param object the statistics data.
  303. */
  304. ConnectionIndicator.prototype.updateConnectionQuality =
  305. function (percent, object) {
  306. if (!percent) {
  307. this.connectionIndicatorContainer.style.display = "none";
  308. this.popover.forceHide();
  309. return;
  310. } else {
  311. if(this.connectionIndicatorContainer.style.display == "none") {
  312. this.connectionIndicatorContainer.style.display = "block";
  313. }
  314. }
  315. if (object) {
  316. this.bandwidth = object.bandwidth;
  317. this.bitrate = object.bitrate;
  318. this.packetLoss = object.packetLoss;
  319. this.transport = object.transport;
  320. if (object.resolution) {
  321. this.resolution = object.resolution;
  322. }
  323. }
  324. let width = qualityToWidth.find(x => percent >= x.percent);
  325. this.fullIcon.style.width = width.width;
  326. if (object && typeof object.isResolutionHD === 'boolean') {
  327. this.isResolutionHD = object.isResolutionHD;
  328. }
  329. this.updateResolutionIndicator();
  330. this.updatePopoverData();
  331. };
  332. /**
  333. * Updates the resolution
  334. * @param resolution the new resolution
  335. */
  336. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  337. this.resolution = resolution;
  338. this.updateResolutionIndicator();
  339. this.updatePopoverData();
  340. };
  341. /**
  342. * Updates the content of the popover if its visible
  343. * @param force to work even if popover is not visible
  344. */
  345. ConnectionIndicator.prototype.updatePopoverData = function (force) {
  346. // generate content, translate it and add it to document only if
  347. // popover is visible or we force to do so.
  348. if(this.popover.popoverShown || force) {
  349. this.popover.updateContent(
  350. `<div class="connection-info">${this.generateText()}</div>`
  351. );
  352. }
  353. };
  354. /**
  355. * Hides the popover
  356. */
  357. ConnectionIndicator.prototype.hide = function () {
  358. this.popover.forceHide();
  359. };
  360. /**
  361. * Hides the indicator
  362. */
  363. ConnectionIndicator.prototype.hideIndicator = function () {
  364. this.connectionIndicatorContainer.style.display = "none";
  365. if(this.popover)
  366. this.popover.forceHide();
  367. };
  368. /**
  369. * Updates the resolution indicator.
  370. */
  371. ConnectionIndicator.prototype.updateResolutionIndicator = function () {
  372. if (this.id !== null
  373. && VideoLayout.isCurrentlyOnLarge(this.id)) {
  374. let showResolutionLabel = false;
  375. if (this.isResolutionHD !== null)
  376. showResolutionLabel = this.isResolutionHD;
  377. else if (this.resolution !== null) {
  378. let resolutions = this.resolution || {};
  379. Object.keys(resolutions).map(function (ssrc) {
  380. const { height } = resolutions[ssrc];
  381. if (height >= config.minHDHeight)
  382. showResolutionLabel = true;
  383. });
  384. }
  385. VideoLayout.updateResolutionLabel(showResolutionLabel);
  386. }
  387. };
  388. /**
  389. * Adds a hover listener to the popover.
  390. */
  391. ConnectionIndicator.prototype.addPopoverHoverListener = function (listener) {
  392. this.popover.addOnHoverPopover(listener);
  393. };
  394. export default ConnectionIndicator;