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.

ConnectionStatsTable.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { translate } from '../../base/i18n';
  4. /**
  5. * React {@code Component} for displaying connection statistics.
  6. *
  7. * @extends Component
  8. */
  9. class ConnectionStatsTable extends Component {
  10. /**
  11. * {@code ConnectionStatsTable} component's property types.
  12. *
  13. * @static
  14. */
  15. static propTypes = {
  16. /**
  17. * Statistics related to bandwidth.
  18. * {{
  19. * download: Number,
  20. * upload: Number
  21. * }}
  22. */
  23. bandwidth: PropTypes.object,
  24. /**
  25. * Statistics related to bitrate.
  26. * {{
  27. * download: Number,
  28. * upload: Number
  29. * }}
  30. */
  31. bitrate: PropTypes.object,
  32. /**
  33. * A message describing the connection quality.
  34. */
  35. connectionSummary: PropTypes.string,
  36. /**
  37. * The end-to-end round-trip-time.
  38. */
  39. e2eRtt: PropTypes.number,
  40. /**
  41. * Statistics related to frame rates for each ssrc.
  42. * {{
  43. * [ ssrc ]: Number
  44. * }}
  45. */
  46. framerate: PropTypes.object,
  47. /**
  48. * Whether or not the statistics are for local video.
  49. */
  50. isLocalVideo: PropTypes.bool,
  51. /**
  52. * Callback to invoke when the show additional stats link is clicked.
  53. */
  54. onShowMore: PropTypes.func,
  55. /**
  56. * Statistics related to packet loss.
  57. * {{
  58. * download: Number,
  59. * upload: Number
  60. * }}
  61. */
  62. packetLoss: PropTypes.object,
  63. /**
  64. * The region.
  65. */
  66. region: PropTypes.string,
  67. /**
  68. * Statistics related to display resolutions for each ssrc.
  69. * {{
  70. * [ ssrc ]: {
  71. * height: Number,
  72. * width: Number
  73. * }
  74. * }}
  75. */
  76. resolution: PropTypes.object,
  77. /**
  78. * Whether or not additional stats about bandwidth and transport should
  79. * be displayed. Will not display even if true for remote participants.
  80. */
  81. shouldShowMore: PropTypes.bool,
  82. /**
  83. * Invoked to obtain translated strings.
  84. */
  85. t: PropTypes.func,
  86. /**
  87. * Statistics related to transports.
  88. */
  89. transport: PropTypes.array
  90. };
  91. /**
  92. * Implements React's {@link Component#render()}.
  93. *
  94. * @inheritdoc
  95. * @returns {ReactElement}
  96. */
  97. render() {
  98. const { isLocalVideo } = this.props;
  99. return (
  100. <div className = 'connection-info'>
  101. { this._renderStatistics() }
  102. { isLocalVideo ? this._renderShowMoreLink() : null }
  103. { isLocalVideo && this.props.shouldShowMore
  104. ? this._renderAdditionalStats() : null }
  105. </div>
  106. );
  107. }
  108. /**
  109. * Creates a table as ReactElement that will display additional statistics
  110. * related to bandwidth and transport.
  111. *
  112. * @private
  113. * @returns {ReactElement}
  114. */
  115. _renderAdditionalStats() {
  116. return (
  117. <table className = 'connection-info__container'>
  118. <tbody>
  119. { this._renderBandwidth() }
  120. { this._renderTransport() }
  121. </tbody>
  122. </table>
  123. );
  124. }
  125. /**
  126. * Creates a table row as a ReactElement for displaying bandwidth related
  127. * statistics.
  128. *
  129. * @private
  130. * @returns {ReactElement}
  131. */
  132. _renderBandwidth() {
  133. const { download, upload } = this.props.bandwidth || {};
  134. return (
  135. <tr>
  136. <td>
  137. { this.props.t('connectionindicator.bandwidth') }
  138. </td>
  139. <td>
  140. <span className = 'connection-info__download'>
  141. &darr;
  142. </span>
  143. { download ? `${download} Kbps` : 'N/A' }
  144. <span className = 'connection-info__upload'>
  145. &uarr;
  146. </span>
  147. { upload ? `${upload} Kbps` : 'N/A' }
  148. </td>
  149. </tr>
  150. );
  151. }
  152. /**
  153. * Creates a a table row as a ReactElement for displaying bitrate related
  154. * statistics.
  155. *
  156. * @private
  157. * @returns {ReactElement}
  158. */
  159. _renderBitrate() {
  160. const { download, upload } = this.props.bitrate || {};
  161. return (
  162. <tr>
  163. <td>
  164. <span>
  165. { this.props.t('connectionindicator.bitrate') }
  166. </span>
  167. </td>
  168. <td>
  169. <span className = 'connection-info__download'>
  170. &darr;
  171. </span>
  172. { download ? `${download} Kbps` : 'N/A' }
  173. <span className = 'connection-info__upload'>
  174. &uarr;
  175. </span>
  176. { upload ? `${upload} Kbps` : 'N/A' }
  177. </td>
  178. </tr>
  179. );
  180. }
  181. /**
  182. * Creates a table row as a ReactElement for displaying a summary message
  183. * about the current connection status.
  184. *
  185. * @private
  186. * @returns {ReactElement}
  187. */
  188. _renderConnectionSummary() {
  189. return (
  190. <tr className = 'connection-info__status'>
  191. <td>
  192. <span>{ this.props.t('connectionindicator.status') }</span>
  193. </td>
  194. <td>{ this.props.connectionSummary }</td>
  195. </tr>
  196. );
  197. }
  198. /**
  199. * Creates a table row as a ReactElement for displaying end-to-end RTT and
  200. * the region.
  201. *
  202. * @returns {ReactElement}
  203. * @private
  204. */
  205. _renderE2eRtt() {
  206. const { e2eRtt, region, t } = this.props;
  207. let str = e2eRtt ? `${e2eRtt.toFixed(0)}ms` : 'N/A';
  208. if (region) {
  209. str += ` (${region})`;
  210. }
  211. return (
  212. <tr>
  213. <td>
  214. <span>{ t('connectionindicator.e2e_rtt') }</span>
  215. </td>
  216. <td>{ str }</td>
  217. </tr>
  218. );
  219. }
  220. /**
  221. * Creates a table row as a ReactElement for displaying frame rate related
  222. * statistics.
  223. *
  224. * @private
  225. * @returns {ReactElement}
  226. */
  227. _renderFrameRate() {
  228. const { framerate, t } = this.props;
  229. const frameRateString = Object.keys(framerate || {})
  230. .map(ssrc => framerate[ssrc])
  231. .join(', ') || 'N/A';
  232. return (
  233. <tr>
  234. <td>
  235. <span>{ t('connectionindicator.framerate') }</span>
  236. </td>
  237. <td>{ frameRateString }</td>
  238. </tr>
  239. );
  240. }
  241. /**
  242. * Creates a tables row as a ReactElement for displaying packet loss related
  243. * statistics.
  244. *
  245. * @private
  246. * @returns {ReactElement}
  247. */
  248. _renderPacketLoss() {
  249. const { packetLoss, t } = this.props;
  250. let packetLossTableData;
  251. if (packetLoss) {
  252. const { download, upload } = packetLoss;
  253. // eslint-disable-next-line no-extra-parens
  254. packetLossTableData = (
  255. <td>
  256. <span className = 'connection-info__download'>
  257. &darr;
  258. </span>
  259. { download === null ? 'N/A' : `${download}%` }
  260. <span className = 'connection-info__upload'>
  261. &uarr;
  262. </span>
  263. { upload === null ? 'N/A' : `${upload}%` }
  264. </td>
  265. );
  266. } else {
  267. packetLossTableData = <td>N/A</td>;
  268. }
  269. return (
  270. <tr>
  271. <td>
  272. <span>
  273. { t('connectionindicator.packetloss') }
  274. </span>
  275. </td>
  276. { packetLossTableData }
  277. </tr>
  278. );
  279. }
  280. /**
  281. * Creates a table row as a ReactElement for displaying resolution related
  282. * statistics.
  283. *
  284. * @private
  285. * @returns {ReactElement}
  286. */
  287. _renderResolution() {
  288. const { resolution, t } = this.props;
  289. const resolutionString = Object.keys(resolution || {})
  290. .map(ssrc => {
  291. const { width, height } = resolution[ssrc];
  292. return `${width}x${height}`;
  293. })
  294. .join(', ') || 'N/A';
  295. return (
  296. <tr>
  297. <td>
  298. <span>{ t('connectionindicator.resolution') }</span>
  299. </td>
  300. <td>{ resolutionString }</td>
  301. </tr>
  302. );
  303. }
  304. /**
  305. * Creates a ReactElement for display a link to toggle showing additional
  306. * statistics.
  307. *
  308. * @private
  309. * @returns {ReactElement}
  310. */
  311. _renderShowMoreLink() {
  312. const translationKey
  313. = this.props.shouldShowMore
  314. ? 'connectionindicator.less'
  315. : 'connectionindicator.more';
  316. return (
  317. <a
  318. className = 'showmore link'
  319. onClick = { this.props.onShowMore } >
  320. { this.props.t(translationKey) }
  321. </a>
  322. );
  323. }
  324. /**
  325. * Creates a table as a ReactElement for displaying connection statistics.
  326. *
  327. * @private
  328. * @returns {ReactElement}
  329. */
  330. _renderStatistics() {
  331. const isRemoteVideo = !this.props.isLocalVideo;
  332. return (
  333. <table className = 'connection-info__container'>
  334. <tbody>
  335. { this._renderConnectionSummary() }
  336. { this._renderBitrate() }
  337. { this._renderPacketLoss() }
  338. { isRemoteVideo ? this._renderE2eRtt() : null }
  339. { this._renderResolution() }
  340. { this._renderFrameRate() }
  341. </tbody>
  342. </table>
  343. );
  344. }
  345. /**
  346. * Creates table rows as ReactElements for displaying transport related
  347. * statistics.
  348. *
  349. * @private
  350. * @returns {ReactElement[]}
  351. */
  352. _renderTransport() {
  353. const { t, transport } = this.props;
  354. if (!transport || transport.length === 0) {
  355. // eslint-disable-next-line no-extra-parens
  356. const NA = (
  357. <tr key = 'address'>
  358. <td>
  359. <span>{ t('connectionindicator.address') }</span>
  360. </td>
  361. <td>
  362. N/A
  363. </td>
  364. </tr>
  365. );
  366. return [ NA ];
  367. }
  368. const data = {
  369. localIP: [],
  370. localPort: [],
  371. remoteIP: [],
  372. remotePort: [],
  373. transportType: []
  374. };
  375. for (let i = 0; i < transport.length; i++) {
  376. const ip = getIP(transport[i].ip);
  377. const localIP = getIP(transport[i].localip);
  378. const localPort = getPort(transport[i].localip);
  379. const port = getPort(transport[i].ip);
  380. if (!data.remoteIP.includes(ip)) {
  381. data.remoteIP.push(ip);
  382. }
  383. if (!data.localIP.includes(localIP)) {
  384. data.localIP.push(localIP);
  385. }
  386. if (!data.localPort.includes(localPort)) {
  387. data.localPort.push(localPort);
  388. }
  389. if (!data.remotePort.includes(port)) {
  390. data.remotePort.push(port);
  391. }
  392. if (!data.transportType.includes(transport[i].type)) {
  393. data.transportType.push(transport[i].type);
  394. }
  395. }
  396. // All of the transports should be either P2P or JVB
  397. let isP2P = false, isTURN = false;
  398. if (transport.length) {
  399. isP2P = transport[0].p2p;
  400. isTURN = transport[0].localCandidateType === 'relay'
  401. || transport[0].remoteCandidateType === 'relay';
  402. }
  403. let additionalData = null;
  404. if (isP2P) {
  405. additionalData = isTURN
  406. ? <span>{ t('connectionindicator.turn') }</span>
  407. : <span>{ t('connectionindicator.peer_to_peer') }</span>;
  408. }
  409. // First show remote statistics, then local, and then transport type.
  410. const tableRowConfigurations = [
  411. {
  412. additionalData,
  413. data: data.remoteIP,
  414. key: 'remoteaddress',
  415. label: t('connectionindicator.remoteaddress',
  416. { count: data.remoteIP.length })
  417. },
  418. {
  419. data: data.remotePort,
  420. key: 'remoteport',
  421. label: t('connectionindicator.remoteport',
  422. { count: transport.length })
  423. },
  424. {
  425. data: data.localIP,
  426. key: 'localaddress',
  427. label: t('connectionindicator.localaddress',
  428. { count: data.localIP.length })
  429. },
  430. {
  431. data: data.localPort,
  432. key: 'localport',
  433. label: t('connectionindicator.localport',
  434. { count: transport.length })
  435. },
  436. {
  437. data: data.transportType,
  438. key: 'transport',
  439. label: t('connectionindicator.transport',
  440. { count: data.transportType.length })
  441. }
  442. ];
  443. return tableRowConfigurations.map(this._renderTransportTableRow);
  444. }
  445. /**
  446. * Creates a table row as a ReactElement for displaying a transport related
  447. * statistic.
  448. *
  449. * @param {Object} config - Describes the contents of the row.
  450. * @param {ReactElement} config.additionalData - Extra data to display next
  451. * to the passed in config.data.
  452. * @param {Array} config.data - The transport statistics to display.
  453. * @param {string} config.key - The ReactElement's key. Must be unique for
  454. * iterating over multiple child rows.
  455. * @param {string} config.label - The text to display describing the data.
  456. * @private
  457. * @returns {ReactElement}
  458. */
  459. _renderTransportTableRow(config) {
  460. const { additionalData, data, key, label } = config;
  461. return (
  462. <tr key = { key }>
  463. <td>
  464. <span>
  465. { label }
  466. </span>
  467. </td>
  468. <td>
  469. { getStringFromArray(data) }
  470. { additionalData || null }
  471. </td>
  472. </tr>
  473. );
  474. }
  475. }
  476. /**
  477. * Utility for getting the IP from a transport statistics object's
  478. * representation of an IP.
  479. *
  480. * @param {string} value - The transport's IP to parse.
  481. * @private
  482. * @returns {string}
  483. */
  484. function getIP(value) {
  485. if (!value) {
  486. return '';
  487. }
  488. return value.substring(0, value.lastIndexOf(':'));
  489. }
  490. /**
  491. * Utility for getting the port from a transport statistics object's
  492. * representation of an IP.
  493. *
  494. * @param {string} value - The transport's IP to parse.
  495. * @private
  496. * @returns {string}
  497. */
  498. function getPort(value) {
  499. if (!value) {
  500. return '';
  501. }
  502. return value.substring(value.lastIndexOf(':') + 1, value.length);
  503. }
  504. /**
  505. * Utility for concatenating values in an array into a comma separated string.
  506. *
  507. * @param {Array} array - Transport statistics to concatenate.
  508. * @private
  509. * @returns {string}
  510. */
  511. function getStringFromArray(array) {
  512. let res = '';
  513. for (let i = 0; i < array.length; i++) {
  514. res += (i === 0 ? '' : ', ') + array[i];
  515. }
  516. return res;
  517. }
  518. export default translate(ConnectionStatsTable);