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

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