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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. const str = `${e2eRtt.toFixed(0)}ms (${region ? region : 'unknown'})`;
  208. return (
  209. <tr>
  210. <td>
  211. <span>{ t('connectionindicator.e2e_rtt') }</span>
  212. </td>
  213. <td>{ str }</td>
  214. </tr>
  215. );
  216. }
  217. /**
  218. * Creates a table row as a ReactElement for displaying frame rate related
  219. * statistics.
  220. *
  221. * @private
  222. * @returns {ReactElement}
  223. */
  224. _renderFrameRate() {
  225. const { framerate, t } = this.props;
  226. const frameRateString = Object.keys(framerate || {})
  227. .map(ssrc => framerate[ssrc])
  228. .join(', ') || 'N/A';
  229. return (
  230. <tr>
  231. <td>
  232. <span>{ t('connectionindicator.framerate') }</span>
  233. </td>
  234. <td>{ frameRateString }</td>
  235. </tr>
  236. );
  237. }
  238. /**
  239. * Creates a tables row as a ReactElement for displaying packet loss related
  240. * statistics.
  241. *
  242. * @private
  243. * @returns {ReactElement}
  244. */
  245. _renderPacketLoss() {
  246. const { packetLoss, t } = this.props;
  247. let packetLossTableData;
  248. if (packetLoss) {
  249. const { download, upload } = packetLoss;
  250. // eslint-disable-next-line no-extra-parens
  251. packetLossTableData = (
  252. <td>
  253. <span className = 'connection-info__download'>
  254. &darr;
  255. </span>
  256. { download === null ? 'N/A' : `${download}%` }
  257. <span className = 'connection-info__upload'>
  258. &uarr;
  259. </span>
  260. { upload === null ? 'N/A' : `${upload}%` }
  261. </td>
  262. );
  263. } else {
  264. packetLossTableData = <td>N/A</td>;
  265. }
  266. return (
  267. <tr>
  268. <td>
  269. <span>
  270. { t('connectionindicator.packetloss') }
  271. </span>
  272. </td>
  273. { packetLossTableData }
  274. </tr>
  275. );
  276. }
  277. /**
  278. * Creates a table row as a ReactElement for displaying resolution related
  279. * statistics.
  280. *
  281. * @private
  282. * @returns {ReactElement}
  283. */
  284. _renderResolution() {
  285. const { resolution, t } = this.props;
  286. const resolutionString = Object.keys(resolution || {})
  287. .map(ssrc => {
  288. const { width, height } = resolution[ssrc];
  289. return `${width}x${height}`;
  290. })
  291. .join(', ') || 'N/A';
  292. return (
  293. <tr>
  294. <td>
  295. <span>{ t('connectionindicator.resolution') }</span>
  296. </td>
  297. <td>{ resolutionString }</td>
  298. </tr>
  299. );
  300. }
  301. /**
  302. * Creates a ReactElement for display a link to toggle showing additional
  303. * statistics.
  304. *
  305. * @private
  306. * @returns {ReactElement}
  307. */
  308. _renderShowMoreLink() {
  309. const translationKey
  310. = this.props.shouldShowMore
  311. ? 'connectionindicator.less'
  312. : 'connectionindicator.more';
  313. return (
  314. <a
  315. className = 'showmore link'
  316. onClick = { this.props.onShowMore } >
  317. { this.props.t(translationKey) }
  318. </a>
  319. );
  320. }
  321. /**
  322. * Creates a table as a ReactElement for displaying connection statistics.
  323. *
  324. * @private
  325. * @returns {ReactElement}
  326. */
  327. _renderStatistics() {
  328. const isRemoteVideo = !this.props.isLocalVideo;
  329. return (
  330. <table className = 'connection-info__container'>
  331. <tbody>
  332. { this._renderConnectionSummary() }
  333. { this._renderBitrate() }
  334. { this._renderPacketLoss() }
  335. { isRemoteVideo ? this._renderE2eRtt() : null }
  336. { this._renderResolution() }
  337. { this._renderFrameRate() }
  338. </tbody>
  339. </table>
  340. );
  341. }
  342. /**
  343. * Creates table rows as ReactElements for displaying transport related
  344. * statistics.
  345. *
  346. * @private
  347. * @returns {ReactElement[]}
  348. */
  349. _renderTransport() {
  350. const { t, transport } = this.props;
  351. if (!transport || transport.length === 0) {
  352. // eslint-disable-next-line no-extra-parens
  353. const NA = (
  354. <tr key = 'address'>
  355. <td>
  356. <span>{ t('connectionindicator.address') }</span>
  357. </td>
  358. <td>
  359. N/A
  360. </td>
  361. </tr>
  362. );
  363. return [ NA ];
  364. }
  365. const data = {
  366. localIP: [],
  367. localPort: [],
  368. remoteIP: [],
  369. remotePort: [],
  370. transportType: []
  371. };
  372. for (let i = 0; i < transport.length; i++) {
  373. const ip = getIP(transport[i].ip);
  374. const localIP = getIP(transport[i].localip);
  375. const localPort = getPort(transport[i].localip);
  376. const port = getPort(transport[i].ip);
  377. if (!data.remoteIP.includes(ip)) {
  378. data.remoteIP.push(ip);
  379. }
  380. if (!data.localIP.includes(localIP)) {
  381. data.localIP.push(localIP);
  382. }
  383. if (!data.localPort.includes(localPort)) {
  384. data.localPort.push(localPort);
  385. }
  386. if (!data.remotePort.includes(port)) {
  387. data.remotePort.push(port);
  388. }
  389. if (!data.transportType.includes(transport[i].type)) {
  390. data.transportType.push(transport[i].type);
  391. }
  392. }
  393. // All of the transports should be either P2P or JVB
  394. let isP2P = false, isTURN = false;
  395. if (transport.length) {
  396. isP2P = transport[0].p2p;
  397. isTURN = transport[0].localCandidateType === 'relay'
  398. || transport[0].remoteCandidateType === 'relay';
  399. }
  400. let additionalData = null;
  401. if (isP2P) {
  402. additionalData = isTURN
  403. ? <span>{ t('connectionindicator.turn') }</span>
  404. : <span>{ t('connectionindicator.peer_to_peer') }</span>;
  405. }
  406. // First show remote statistics, then local, and then transport type.
  407. const tableRowConfigurations = [
  408. {
  409. additionalData,
  410. data: data.remoteIP,
  411. key: 'remoteaddress',
  412. label: t('connectionindicator.remoteaddress',
  413. { count: data.remoteIP.length })
  414. },
  415. {
  416. data: data.remotePort,
  417. key: 'remoteport',
  418. label: t('connectionindicator.remoteport',
  419. { count: transport.length })
  420. },
  421. {
  422. data: data.localIP,
  423. key: 'localaddress',
  424. label: t('connectionindicator.localaddress',
  425. { count: data.localIP.length })
  426. },
  427. {
  428. data: data.localPort,
  429. key: 'localport',
  430. label: t('connectionindicator.localport',
  431. { count: transport.length })
  432. },
  433. {
  434. data: data.transportType,
  435. key: 'transport',
  436. label: t('connectionindicator.transport',
  437. { count: data.transportType.length })
  438. }
  439. ];
  440. return tableRowConfigurations.map(this._renderTransportTableRow);
  441. }
  442. /**
  443. * Creates a table row as a ReactElement for displaying a transport related
  444. * statistic.
  445. *
  446. * @param {Object} config - Describes the contents of the row.
  447. * @param {ReactElement} config.additionalData - Extra data to display next
  448. * to the passed in config.data.
  449. * @param {Array} config.data - The transport statistics to display.
  450. * @param {string} config.key - The ReactElement's key. Must be unique for
  451. * iterating over multiple child rows.
  452. * @param {string} config.label - The text to display describing the data.
  453. * @private
  454. * @returns {ReactElement}
  455. */
  456. _renderTransportTableRow(config) {
  457. const { additionalData, data, key, label } = config;
  458. return (
  459. <tr key = { key }>
  460. <td>
  461. <span>
  462. { label }
  463. </span>
  464. </td>
  465. <td>
  466. { getStringFromArray(data) }
  467. { additionalData || null }
  468. </td>
  469. </tr>
  470. );
  471. }
  472. }
  473. /**
  474. * Utility for getting the IP from a transport statistics object's
  475. * representation of an IP.
  476. *
  477. * @param {string} value - The transport's IP to parse.
  478. * @private
  479. * @returns {string}
  480. */
  481. function getIP(value) {
  482. if (!value) {
  483. return '';
  484. }
  485. return value.substring(0, value.lastIndexOf(':'));
  486. }
  487. /**
  488. * Utility for getting the port from a transport statistics object's
  489. * representation of an IP.
  490. *
  491. * @param {string} value - The transport's IP to parse.
  492. * @private
  493. * @returns {string}
  494. */
  495. function getPort(value) {
  496. if (!value) {
  497. return '';
  498. }
  499. return value.substring(value.lastIndexOf(':') + 1, value.length);
  500. }
  501. /**
  502. * Utility for concatenating values in an array into a comma separated string.
  503. *
  504. * @param {Array} array - Transport statistics to concatenate.
  505. * @private
  506. * @returns {string}
  507. */
  508. function getStringFromArray(array) {
  509. let res = '';
  510. for (let i = 0; i < array.length; i++) {
  511. res += (i === 0 ? '' : ', ') + array[i];
  512. }
  513. return res;
  514. }
  515. export default translate(ConnectionStatsTable);