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.

VideoQualityLabel.web.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import AKInlineDialog from '@atlaskit/inline-dialog';
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { translate } from '../../base/i18n';
  5. import { VideoQualityDialog } from './';
  6. import {
  7. VIDEO_QUALITY_LEVELS
  8. } from '../../base/conference';
  9. const { HIGH, STANDARD, LOW } = VIDEO_QUALITY_LEVELS;
  10. /**
  11. * Expected video resolutions placed into an array, sorted from lowest to
  12. * highest resolution.
  13. *
  14. * @type {number[]}
  15. */
  16. const RESOLUTIONS
  17. = Object.values(VIDEO_QUALITY_LEVELS).sort((a, b) => a - b);
  18. /**
  19. * A map of video resolution (number) to translation key.
  20. *
  21. * @type {Object}
  22. */
  23. const RESOLUTION_TO_TRANSLATION_KEY = {
  24. [HIGH]: 'videoStatus.hd',
  25. [STANDARD]: 'videoStatus.sd',
  26. [LOW]: 'videoStatus.ld'
  27. };
  28. /**
  29. * React {@code Component} responsible for displaying a label that indicates
  30. * the displayed video state of the current conference. {@code AudioOnlyLabel}
  31. * will display when the conference is in audio only mode. {@code HDVideoLabel}
  32. * will display if not in audio only mode and a high-definition large video is
  33. * being displayed.
  34. */
  35. export class VideoQualityLabel extends Component {
  36. /**
  37. * {@code VideoQualityLabel}'s property types.
  38. *
  39. * @static
  40. */
  41. static propTypes = {
  42. /**
  43. * Whether or not the conference is in audio only mode.
  44. */
  45. _audioOnly: React.PropTypes.bool,
  46. /**
  47. * Whether or not a connection to a conference has been established.
  48. */
  49. _conferenceStarted: React.PropTypes.bool,
  50. /**
  51. * Whether or not the filmstrip is displayed with remote videos. Used to
  52. * determine display classes to set.
  53. */
  54. _filmstripVisible: React.PropTypes.bool,
  55. /**
  56. * Whether or note remote videos are visible in the filmstrip,
  57. * regardless of count. Used to determine display classes to set.
  58. */
  59. _remoteVideosVisible: React.PropTypes.bool,
  60. /**
  61. * The current video resolution (height) to display a label for.
  62. */
  63. _resolution: React.PropTypes.number,
  64. /**
  65. * Invoked to obtain translated strings.
  66. */
  67. t: React.PropTypes.func
  68. };
  69. /**
  70. * Initializes a new {@code VideoQualityLabel} instance.
  71. *
  72. * @param {Object} props - The read-only React Component props with which
  73. * the new instance is to be initialized.
  74. */
  75. constructor(props) {
  76. super(props);
  77. this.state = {
  78. /**
  79. * Whether or not the {@code VideoQualityDialog} is displayed.
  80. *
  81. * @type {boolean}
  82. */
  83. showVideoQualityDialog: false,
  84. /**
  85. * Whether or not the filmstrip is transitioning from not visible
  86. * to visible. Used to set a transition class for animation.
  87. *
  88. * @type {boolean}
  89. */
  90. togglingToVisible: false
  91. };
  92. // Bind event handlers so they are only bound once for every instance.
  93. this._onDialogClose = this._onDialogClose.bind(this);
  94. this._onDialogToggle = this._onDialogToggle.bind(this);
  95. }
  96. /**
  97. * Updates the state for whether or not the filmstrip is being toggled to
  98. * display after having being hidden.
  99. *
  100. * @inheritdoc
  101. * @param {Object} nextProps - The read-only props which this Component will
  102. * receive.
  103. * @returns {void}
  104. */
  105. componentWillReceiveProps(nextProps) {
  106. this.setState({
  107. togglingToVisible: nextProps._filmstripVisible
  108. && !this.props._filmstripVisible
  109. });
  110. }
  111. /**
  112. * Implements React's {@link Component#render()}.
  113. *
  114. * @inheritdoc
  115. * @returns {ReactElement}
  116. */
  117. render() {
  118. const {
  119. _audioOnly,
  120. _conferenceStarted,
  121. _filmstripVisible,
  122. _remoteVideosVisible,
  123. _resolution
  124. } = this.props;
  125. // FIXME The _conferenceStarted check is used to be defensive against
  126. // toggling audio only mode while there is no conference and hides the
  127. // need for error handling around audio only mode toggling.
  128. if (!_conferenceStarted) {
  129. return null;
  130. }
  131. // Determine which classes should be set on the component. These classes
  132. // will used to help with animations and setting position.
  133. const baseClasses = 'video-state-indicator moveToCorner';
  134. const filmstrip
  135. = _filmstripVisible ? 'with-filmstrip' : 'without-filmstrip';
  136. const remoteVideosVisible = _remoteVideosVisible
  137. ? 'with-remote-videos'
  138. : 'without-remote-videos';
  139. const opening = this.state.togglingToVisible ? 'opening' : '';
  140. const classNames
  141. = `${baseClasses} ${filmstrip} ${remoteVideosVisible} ${opening}`;
  142. return (
  143. <div
  144. className = { classNames }
  145. id = 'videoResolutionLabel'
  146. onClick = { this._onDialogToggle }>
  147. <AKInlineDialog
  148. content = { <VideoQualityDialog /> }
  149. isOpen = { this.state.showVideoQualityDialog }
  150. onClose = { this._onDialogClose }
  151. position = { 'left top' }>
  152. <div className = 'video-quality-label-status'>
  153. { _audioOnly
  154. ? <i className = 'icon-visibility-off' />
  155. : this._mapResolutionToTranslation(_resolution) }
  156. </div>
  157. </AKInlineDialog>
  158. </div>
  159. );
  160. }
  161. /**
  162. * Matches the passed in resolution with a translation key for describing
  163. * the resolution. The passed in resolution will be matched with a known
  164. * resolution that it is at least greater than or equal to.
  165. *
  166. * @param {number} resolution - The video height to match with a
  167. * translation.
  168. * @private
  169. * @returns {string}
  170. */
  171. _mapResolutionToTranslation(resolution) {
  172. // Set the default matching resolution of the lowest just in case a
  173. // match is not found.
  174. let highestMatchingResolution = RESOLUTIONS[0];
  175. for (let i = 0; i < RESOLUTIONS.length; i++) {
  176. const knownResolution = RESOLUTIONS[i];
  177. if (resolution >= knownResolution) {
  178. highestMatchingResolution = knownResolution;
  179. } else {
  180. break;
  181. }
  182. }
  183. return this.props.t(
  184. RESOLUTION_TO_TRANSLATION_KEY[highestMatchingResolution]);
  185. }
  186. /**
  187. * Toggles the display of the {@code VideoQualityDialog}.
  188. *
  189. * @private
  190. * @returns {void}
  191. */
  192. _onDialogToggle() {
  193. this.setState({
  194. showVideoQualityDialog: !this.state.showVideoQualityDialog
  195. });
  196. }
  197. /**
  198. * Hides the attached inline dialog.
  199. *
  200. * @private
  201. * @returns {void}
  202. */
  203. _onDialogClose() {
  204. this.setState({ showVideoQualityDialog: false });
  205. }
  206. }
  207. /**
  208. * Maps (parts of) the Redux state to the associated {@code VideoQualityLabel}'s
  209. * props.
  210. *
  211. * @param {Object} state - The Redux state.
  212. * @private
  213. * @returns {{
  214. * _audioOnly: boolean,
  215. * _conferenceStarted: boolean,
  216. * _filmstripVisible: true,
  217. * _remoteVideosVisible: boolean,
  218. * _resolution: number
  219. * }}
  220. */
  221. function _mapStateToProps(state) {
  222. const {
  223. audioOnly,
  224. conference
  225. } = state['features/base/conference'];
  226. const {
  227. remoteVideosVisible,
  228. visible
  229. } = state['features/filmstrip'];
  230. const {
  231. resolution
  232. } = state['features/large-video'];
  233. return {
  234. _audioOnly: audioOnly,
  235. _conferenceStarted: Boolean(conference),
  236. _filmstripVisible: visible,
  237. _remoteVideosVisible: remoteVideosVisible,
  238. _resolution: resolution
  239. };
  240. }
  241. export default translate(connect(_mapStateToProps)(VideoQualityLabel));