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

ConnectionIndicator.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. var JitsiPopover = require("../util/JitsiPopover");
  2. /**
  3. * Constructs new connection indicator.
  4. * @param videoContainer the video container associated with the indicator.
  5. * @constructor
  6. */
  7. function ConnectionIndicator(videoContainer, jid, VideoLayout)
  8. {
  9. this.videoContainer = videoContainer;
  10. this.bandwidth = null;
  11. this.packetLoss = null;
  12. this.bitrate = null;
  13. this.showMoreValue = false;
  14. this.resolution = null;
  15. this.transport = [];
  16. this.popover = null;
  17. this.jid = jid;
  18. this.create();
  19. this.videoLayout = VideoLayout;
  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. {
  40. return value.substring(0, value.lastIndexOf(":"));
  41. };
  42. ConnectionIndicator.getPort = function(value)
  43. {
  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. {
  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, resolution, i;
  60. var translate = APP.translation.translateString;
  61. if(this.bitrate === null)
  62. {
  63. downloadBitrate = "N/A";
  64. uploadBitrate = "N/A";
  65. }
  66. else
  67. {
  68. downloadBitrate =
  69. this.bitrate.download? this.bitrate.download + " Kbps" : "N/A";
  70. uploadBitrate =
  71. this.bitrate.upload? this.bitrate.upload + " Kbps" : "N/A";
  72. }
  73. if(this.packetLoss === null)
  74. {
  75. packetLoss = "N/A";
  76. }
  77. else
  78. {
  79. packetLoss = "<span class='jitsipopover_green'>&darr;</span>" +
  80. (this.packetLoss.download !== null? this.packetLoss.download : "N/A") +
  81. "% <span class='jitsipopover_orange'>&uarr;</span>" +
  82. (this.packetLoss.upload !== null? this.packetLoss.upload : "N/A") + "%";
  83. }
  84. var resolutionValue = null;
  85. if(this.resolution && this.jid != null)
  86. {
  87. var keys = Object.keys(this.resolution);
  88. if(keys.length == 1)
  89. {
  90. for(var ssrc in this.resolution)
  91. {
  92. resolutionValue = this.resolution[ssrc];
  93. }
  94. }
  95. else if(keys.length > 1)
  96. {
  97. var displayedSsrc = APP.simulcast.getReceivingSSRC(this.jid);
  98. resolutionValue = this.resolution[displayedSsrc];
  99. }
  100. }
  101. if(this.jid === null)
  102. {
  103. resolution = "";
  104. if(this.resolution === null || !Object.keys(this.resolution) ||
  105. Object.keys(this.resolution).length === 0)
  106. {
  107. resolution = "N/A";
  108. }
  109. else
  110. for(i in this.resolution)
  111. {
  112. resolutionValue = this.resolution[i];
  113. if(resolutionValue)
  114. {
  115. if(resolutionValue.height &&
  116. resolutionValue.width)
  117. {
  118. resolution += (resolution === ""? "" : ", ") +
  119. resolutionValue.width + "x" +
  120. resolutionValue.height;
  121. }
  122. }
  123. }
  124. }
  125. else if(!resolutionValue ||
  126. !resolutionValue.height ||
  127. !resolutionValue.width)
  128. {
  129. resolution = "N/A";
  130. }
  131. else
  132. {
  133. resolution = resolutionValue.width + "x" + resolutionValue.height;
  134. }
  135. var result = "<table style='width:100%'>" +
  136. "<tr>" +
  137. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.bitrate'>" +
  138. translate("connectionindicator.bitrate") + "</span></td>" +
  139. "<td><span class='jitsipopover_green'>&darr;</span>" +
  140. downloadBitrate + " <span class='jitsipopover_orange'>&uarr;</span>" +
  141. uploadBitrate + "</td>" +
  142. "</tr><tr>" +
  143. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.packetloss'>" +
  144. translate("connectionindicator.packetloss") + "</span></td>" +
  145. "<td>" + packetLoss + "</td>" +
  146. "</tr><tr>" +
  147. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.resolution'>" +
  148. translate("connectionindicator.resolution") + "</span></td>" +
  149. "<td>" + resolution + "</td></tr></table>";
  150. if(this.videoContainer.id == "localVideoContainer") {
  151. result += "<div class=\"jitsipopover_showmore\" " +
  152. "onclick = \"APP.UI.connectionIndicatorShowMore('" +
  153. this.videoContainer.id + "')\" data-i18n='connectionindicator." +
  154. (this.showMoreValue ? "less" : "more") + "'>" +
  155. translate("connectionindicator." + (this.showMoreValue ? "less" : "more")) +
  156. "</div><br />";
  157. }
  158. if(this.showMoreValue)
  159. {
  160. var downloadBandwidth, uploadBandwidth, transport;
  161. if(this.bandwidth === null)
  162. {
  163. downloadBandwidth = "N/A";
  164. uploadBandwidth = "N/A";
  165. }
  166. else
  167. {
  168. downloadBandwidth = this.bandwidth.download?
  169. this.bandwidth.download + " Kbps" :
  170. "N/A";
  171. uploadBandwidth = this.bandwidth.upload?
  172. this.bandwidth.upload + " Kbps" :
  173. "N/A";
  174. }
  175. if(!this.transport || this.transport.length === 0)
  176. {
  177. transport = "<tr>" +
  178. "<td><span class='jitsipopover_blue' " +
  179. "data-i18n='connectionindicator.address'>" +
  180. translate("connectionindicator.address") + "</span></td>" +
  181. "<td> N/A</td></tr>";
  182. }
  183. else
  184. {
  185. var data = {remoteIP: [], localIP:[], remotePort:[], localPort:[]};
  186. for(i = 0; i < this.transport.length; i++)
  187. {
  188. var ip = ConnectionIndicator.getIP(this.transport[i].ip);
  189. var port = ConnectionIndicator.getPort(this.transport[i].ip);
  190. var localIP =
  191. ConnectionIndicator.getIP(this.transport[i].localip);
  192. var localPort =
  193. ConnectionIndicator.getPort(this.transport[i].localip);
  194. if(data.remoteIP.indexOf(ip) == -1)
  195. {
  196. data.remoteIP.push(ip);
  197. }
  198. if(data.remotePort.indexOf(port) == -1)
  199. {
  200. data.remotePort.push(port);
  201. }
  202. if(data.localIP.indexOf(localIP) == -1)
  203. {
  204. data.localIP.push(localIP);
  205. }
  206. if(data.localPort.indexOf(localPort) == -1)
  207. {
  208. data.localPort.push(localPort);
  209. }
  210. }
  211. var local_address_key = "connectionindicator." +
  212. (data.localIP.length > 1? "localaddresses" : "localaddress");
  213. var remote_address_key = "connectionindicator." +
  214. (data.remoteIP.length > 1? "remoteaddresses" : "remoteaddress");
  215. var localTransport =
  216. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  217. local_address_key +"'>" +
  218. translate(local_address_key) + "</span></td><td> " +
  219. ConnectionIndicator.getStringFromArray(data.localIP) +
  220. "</td></tr>";
  221. transport =
  222. "<tr><td><span class='jitsipopover_blue' data-i18n='" +
  223. remote_address_key + "'>" +
  224. translate(remote_address_key) + "</span></td><td> " +
  225. ConnectionIndicator.getStringFromArray(data.remoteIP) +
  226. "</td></tr>";
  227. var key_remote = "connectionindicator.",
  228. key_local = "connectionindicator.";
  229. if(this.transport.length > 1)
  230. {
  231. key_remote += "remoteports";
  232. key_local += "localports";
  233. }
  234. else
  235. {
  236. key_remote += "remoteport";
  237. key_local += "localport";
  238. }
  239. transport += "<tr>" +
  240. "<td>" +
  241. "<span class='jitsipopover_blue' data-i18n='" + key_remote +
  242. "'>" +
  243. translate(key_remote) + "</span></td><td>";
  244. localTransport += "<tr>" +
  245. "<td>" +
  246. "<span class='jitsipopover_blue' data-i18n='" + key_local +
  247. "'>" +
  248. translate(key_local) + "</span></td><td>";
  249. transport +=
  250. ConnectionIndicator.getStringFromArray(data.remotePort);
  251. localTransport +=
  252. ConnectionIndicator.getStringFromArray(data.localPort);
  253. transport += "</td></tr>";
  254. transport += localTransport + "</td></tr>";
  255. transport +="<tr>" +
  256. "<td><span class='jitsipopover_blue' data-i18n='connectionindicator.transport'>" +
  257. translate("connectionindicator.transport") + "</span></td>" +
  258. "<td>" + this.transport[0].type + "</td></tr>";
  259. }
  260. result += "<table style='width:100%'>" +
  261. "<tr>" +
  262. "<td>" +
  263. "<span class='jitsipopover_blue' data-i18n='connectionindicator.bandwidth'>" +
  264. translate("connectionindicator.bandwidth") + "</span>" +
  265. "</td><td>" +
  266. "<span class='jitsipopover_green'>&darr;</span>" +
  267. downloadBandwidth +
  268. " <span class='jitsipopover_orange'>&uarr;</span>" +
  269. uploadBandwidth + "</td></tr>";
  270. result += transport + "</table>";
  271. }
  272. return result;
  273. };
  274. /**
  275. * Shows or hide the additional information.
  276. */
  277. ConnectionIndicator.prototype.showMore = function () {
  278. this.showMoreValue = !this.showMoreValue;
  279. this.updatePopoverData();
  280. };
  281. function createIcon(classes)
  282. {
  283. var icon = document.createElement("span");
  284. for(var i in classes)
  285. {
  286. icon.classList.add(classes[i]);
  287. }
  288. icon.appendChild(
  289. document.createElement("i")).classList.add("icon-connection");
  290. return icon;
  291. }
  292. /**
  293. * Creates the indicator
  294. */
  295. ConnectionIndicator.prototype.create = function () {
  296. this.connectionIndicatorContainer = document.createElement("div");
  297. this.connectionIndicatorContainer.className = "connectionindicator";
  298. this.connectionIndicatorContainer.style.display = "none";
  299. this.videoContainer.appendChild(this.connectionIndicatorContainer);
  300. this.popover = new JitsiPopover(
  301. $("#" + this.videoContainer.id + " > .connectionindicator"),
  302. {content: "<div class=\"connection_info\" data-i18n='connectionindicator.na'>" +
  303. APP.translation.translateString("connectionindicator.na") + "</div>",
  304. skin: "black"});
  305. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  306. createIcon(["connection", "connection_empty"]));
  307. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  308. createIcon(["connection", "connection_full"]));
  309. };
  310. /**
  311. * Removes the indicator
  312. */
  313. ConnectionIndicator.prototype.remove = function()
  314. {
  315. this.connectionIndicatorContainer.remove();
  316. this.popover.forceHide();
  317. };
  318. /**
  319. * Updates the data of the indicator
  320. * @param percent the percent of connection quality
  321. * @param object the statistics data.
  322. */
  323. ConnectionIndicator.prototype.updateConnectionQuality =
  324. function (percent, object) {
  325. if(percent === null)
  326. {
  327. this.connectionIndicatorContainer.style.display = "none";
  328. this.popover.forceHide();
  329. return;
  330. }
  331. else
  332. {
  333. if(this.connectionIndicatorContainer.style.display == "none") {
  334. this.connectionIndicatorContainer.style.display = "block";
  335. this.videoLayout.updateMutePosition(this.videoContainer.id);
  336. }
  337. }
  338. this.bandwidth = object.bandwidth;
  339. this.bitrate = object.bitrate;
  340. this.packetLoss = object.packetLoss;
  341. this.transport = object.transport;
  342. if(object.resolution)
  343. {
  344. this.resolution = object.resolution;
  345. }
  346. for(var quality in ConnectionIndicator.connectionQualityValues)
  347. {
  348. if(percent >= quality)
  349. {
  350. this.fullIcon.style.width =
  351. ConnectionIndicator.connectionQualityValues[quality];
  352. }
  353. }
  354. this.updatePopoverData();
  355. };
  356. /**
  357. * Updates the resolution
  358. * @param resolution the new resolution
  359. */
  360. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  361. this.resolution = resolution;
  362. this.updatePopoverData();
  363. };
  364. /**
  365. * Updates the content of the popover
  366. */
  367. ConnectionIndicator.prototype.updatePopoverData = function () {
  368. this.popover.updateContent(
  369. "<div class=\"connection_info\">" + this.generateText() + "</div>");
  370. APP.translation.translateElement($(".connection_info"));
  371. };
  372. /**
  373. * Hides the popover
  374. */
  375. ConnectionIndicator.prototype.hide = function () {
  376. this.popover.forceHide();
  377. };
  378. /**
  379. * Hides the indicator
  380. */
  381. ConnectionIndicator.prototype.hideIndicator = function () {
  382. this.connectionIndicatorContainer.style.display = "none";
  383. if(this.popover)
  384. this.popover.forceHide();
  385. };
  386. module.exports = ConnectionIndicator;