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.

Toolbox.native.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { View } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import {
  6. AUDIO_MUTE,
  7. VIDEO_MUTE,
  8. createToolbarEvent,
  9. sendAnalytics
  10. } from '../../analytics';
  11. import { toggleAudioOnly } from '../../base/conference';
  12. import {
  13. MEDIA_TYPE,
  14. setAudioMuted,
  15. setVideoMuted,
  16. toggleCameraFacingMode,
  17. VIDEO_MUTISM_AUTHORITY
  18. } from '../../base/media';
  19. import { Container } from '../../base/react';
  20. import {
  21. isNarrowAspectRatio,
  22. makeAspectRatioAware
  23. } from '../../base/responsive-ui';
  24. import { ColorPalette } from '../../base/styles';
  25. import { beginRoomLockRequest } from '../../room-lock';
  26. import { beginShareRoom } from '../../share-room';
  27. import {
  28. abstractMapDispatchToProps,
  29. abstractMapStateToProps
  30. } from '../functions';
  31. import AudioRouteButton from './AudioRouteButton';
  32. import styles from './styles';
  33. import ToolbarButton from './ToolbarButton';
  34. /**
  35. * The indicator which determines (at bundle time) whether there should be a
  36. * {@code ToolbarButton} in {@code Toolbox} to expose the functionality of the
  37. * feature share-room in the user interface of the app.
  38. *
  39. * @private
  40. * @type {boolean}
  41. */
  42. const _SHARE_ROOM_TOOLBAR_BUTTON = true;
  43. /**
  44. * Implements the conference toolbox on React Native.
  45. */
  46. class Toolbox extends Component {
  47. /**
  48. * Toolbox component's property types.
  49. *
  50. * @static
  51. */
  52. static propTypes = {
  53. /**
  54. * Flag showing that audio is muted.
  55. */
  56. _audioMuted: PropTypes.bool,
  57. /**
  58. * Flag showing whether the audio-only mode is in use.
  59. */
  60. _audioOnly: PropTypes.bool,
  61. /**
  62. * The indicator which determines whether the toolbox is enabled.
  63. */
  64. _enabled: PropTypes.bool,
  65. /**
  66. * Flag showing whether room is locked.
  67. */
  68. _locked: PropTypes.bool,
  69. /**
  70. * Handler for hangup.
  71. */
  72. _onHangup: PropTypes.func,
  73. /**
  74. * Sets the lock i.e. password protection of the conference/room.
  75. */
  76. _onRoomLock: PropTypes.func,
  77. /**
  78. * Begins the UI procedure to share the conference/room URL.
  79. */
  80. _onShareRoom: PropTypes.func,
  81. /**
  82. * Toggles the audio-only flag of the conference.
  83. */
  84. _onToggleAudioOnly: PropTypes.func,
  85. /**
  86. * Switches between the front/user-facing and back/environment-facing
  87. * cameras.
  88. */
  89. _onToggleCameraFacingMode: PropTypes.func,
  90. /**
  91. * Flag showing whether video is muted.
  92. */
  93. _videoMuted: PropTypes.bool,
  94. /**
  95. * Flag showing whether toolbar is visible.
  96. */
  97. _visible: PropTypes.bool,
  98. dispatch: PropTypes.func
  99. };
  100. /**
  101. * Initializes a new {@code Toolbox} instance.
  102. *
  103. * @param {Object} props - The read-only React {@code Component} props with
  104. * which the new instance is to be initialized.
  105. */
  106. constructor(props) {
  107. super(props);
  108. // Bind event handlers so they are only bound once per instance.
  109. this._onToggleAudio = this._onToggleAudio.bind(this);
  110. this._onToggleVideo = this._onToggleVideo.bind(this);
  111. }
  112. /**
  113. * Implements React's {@link Component#render()}.
  114. *
  115. * @inheritdoc
  116. * @returns {ReactElement}
  117. */
  118. render() {
  119. if (!this.props._enabled) {
  120. return null;
  121. }
  122. const toolboxStyle
  123. = isNarrowAspectRatio(this)
  124. ? styles.toolboxNarrow
  125. : styles.toolboxWide;
  126. return (
  127. <Container
  128. style = { toolboxStyle }
  129. visible = { this.props._visible } >
  130. { this._renderToolbars() }
  131. </Container>
  132. );
  133. }
  134. /**
  135. * Gets the styles for a button that toggles the mute state of a specific
  136. * media type.
  137. *
  138. * @param {string} mediaType - The {@link MEDIA_TYPE} associated with the
  139. * button to get styles for.
  140. * @protected
  141. * @returns {{
  142. * iconName: string,
  143. * iconStyle: Object,
  144. * style: Object
  145. * }}
  146. */
  147. _getMuteButtonStyles(mediaType) {
  148. let iconName;
  149. let iconStyle;
  150. let style;
  151. if (this.props[`_${mediaType}Muted`]) {
  152. iconName = this[`${mediaType}MutedIcon`];
  153. iconStyle = styles.whitePrimaryToolbarButtonIcon;
  154. style = styles.whitePrimaryToolbarButton;
  155. } else {
  156. iconName = this[`${mediaType}Icon`];
  157. iconStyle = styles.primaryToolbarButtonIcon;
  158. style = styles.primaryToolbarButton;
  159. }
  160. return {
  161. iconName,
  162. iconStyle,
  163. style
  164. };
  165. }
  166. /**
  167. * Dispatches an action to toggle the mute state of the audio/microphone.
  168. *
  169. * @private
  170. * @returns {void}
  171. */
  172. _onToggleAudio() {
  173. const mute = !this.props._audioMuted;
  174. sendAnalytics(createToolbarEvent(
  175. AUDIO_MUTE,
  176. {
  177. enable: mute
  178. }));
  179. // The user sees the reality i.e. the state of base/tracks and intends
  180. // to change reality by tapping on the respective button i.e. the user
  181. // sets the state of base/media. Whether the user's intention will turn
  182. // into reality is a whole different story which is of no concern to the
  183. // tapping.
  184. this.props.dispatch(
  185. setAudioMuted(
  186. mute,
  187. VIDEO_MUTISM_AUTHORITY.USER,
  188. /* ensureTrack */ true));
  189. }
  190. /**
  191. * Dispatches an action to toggle the mute state of the video/camera.
  192. *
  193. * @private
  194. * @returns {void}
  195. */
  196. _onToggleVideo() {
  197. const mute = !this.props._videoMuted;
  198. sendAnalytics(createToolbarEvent(
  199. VIDEO_MUTE,
  200. {
  201. enable: mute
  202. }));
  203. // The user sees the reality i.e. the state of base/tracks and intends
  204. // to change reality by tapping on the respective button i.e. the user
  205. // sets the state of base/media. Whether the user's intention will turn
  206. // into reality is a whole different story which is of no concern to the
  207. // tapping.
  208. this.props.dispatch(
  209. setVideoMuted(
  210. !this.props._videoMuted,
  211. VIDEO_MUTISM_AUTHORITY.USER,
  212. /* ensureTrack */ true));
  213. }
  214. /**
  215. * Renders the toolbar which contains the primary buttons such as hangup,
  216. * audio and video mute.
  217. *
  218. * @private
  219. * @returns {ReactElement}
  220. */
  221. _renderPrimaryToolbar() {
  222. const audioButtonStyles = this._getMuteButtonStyles(MEDIA_TYPE.AUDIO);
  223. const videoButtonStyles = this._getMuteButtonStyles(MEDIA_TYPE.VIDEO);
  224. /* eslint-disable react/jsx-handler-names */
  225. return (
  226. <View
  227. key = 'primaryToolbar'
  228. style = { styles.primaryToolbar }>
  229. <ToolbarButton
  230. iconName = { audioButtonStyles.iconName }
  231. iconStyle = { audioButtonStyles.iconStyle }
  232. onClick = { this._onToggleAudio }
  233. style = { audioButtonStyles.style } />
  234. <ToolbarButton
  235. accessibilityLabel = 'Hangup'
  236. iconName = 'hangup'
  237. iconStyle = { styles.whitePrimaryToolbarButtonIcon }
  238. onClick = { this.props._onHangup }
  239. style = { styles.hangup }
  240. underlayColor = { ColorPalette.buttonUnderlay } />
  241. <ToolbarButton
  242. disabled = { this.props._audioOnly }
  243. iconName = { videoButtonStyles.iconName }
  244. iconStyle = { videoButtonStyles.iconStyle }
  245. onClick = { this._onToggleVideo }
  246. style = { videoButtonStyles.style } />
  247. </View>
  248. );
  249. /* eslint-enable react/jsx-handler-names */
  250. }
  251. /**
  252. * Renders the toolbar which contains the secondary buttons such as toggle
  253. * camera facing mode.
  254. *
  255. * @private
  256. * @returns {ReactElement}
  257. */
  258. _renderSecondaryToolbar() {
  259. const iconStyle = styles.secondaryToolbarButtonIcon;
  260. const style = styles.secondaryToolbarButton;
  261. const underlayColor = 'transparent';
  262. const {
  263. _audioOnly: audioOnly,
  264. _videoMuted: videoMuted
  265. } = this.props;
  266. /* eslint-disable react/jsx-curly-spacing,react/jsx-handler-names */
  267. return (
  268. <View
  269. key = 'secondaryToolbar'
  270. style = { styles.secondaryToolbar }>
  271. {
  272. AudioRouteButton
  273. && <AudioRouteButton
  274. iconName = { 'volume' }
  275. iconStyle = { iconStyle }
  276. style = { style }
  277. underlayColor = { underlayColor } />
  278. }
  279. <ToolbarButton
  280. disabled = { audioOnly || videoMuted }
  281. iconName = 'switch-camera'
  282. iconStyle = { iconStyle }
  283. onClick = { this.props._onToggleCameraFacingMode }
  284. style = { style }
  285. underlayColor = { underlayColor } />
  286. <ToolbarButton
  287. iconName = { audioOnly ? 'visibility-off' : 'visibility' }
  288. iconStyle = { iconStyle }
  289. onClick = { this.props._onToggleAudioOnly }
  290. style = { style }
  291. underlayColor = { underlayColor } />
  292. <ToolbarButton
  293. iconName = {
  294. this.props._locked ? 'security-locked' : 'security'
  295. }
  296. iconStyle = { iconStyle }
  297. onClick = { this.props._onRoomLock }
  298. style = { style }
  299. underlayColor = { underlayColor } />
  300. {
  301. _SHARE_ROOM_TOOLBAR_BUTTON
  302. && <ToolbarButton
  303. iconName = 'link'
  304. iconStyle = { iconStyle }
  305. onClick = { this.props._onShareRoom }
  306. style = { style }
  307. underlayColor = { underlayColor } />
  308. }
  309. </View>
  310. );
  311. /* eslint-enable react/jsx-curly-spacing,react/jsx-handler-names */
  312. }
  313. /**
  314. * Renders the primary and the secondary toolbars.
  315. *
  316. * @private
  317. * @returns {[ReactElement, ReactElement]}
  318. */
  319. _renderToolbars() {
  320. return [
  321. this._renderSecondaryToolbar(),
  322. this._renderPrimaryToolbar()
  323. ];
  324. }
  325. }
  326. /**
  327. * Additional properties for various icons, which are now platform-dependent.
  328. * This is done to have common logic of generating styles for web and native.
  329. * TODO As soon as we have common font sets for web and native, this will no
  330. * longer be required.
  331. */
  332. Object.assign(Toolbox.prototype, {
  333. audioIcon: 'microphone',
  334. audioMutedIcon: 'mic-disabled',
  335. videoIcon: 'camera',
  336. videoMutedIcon: 'camera-disabled'
  337. });
  338. /**
  339. * Maps actions to React component props.
  340. *
  341. * @param {Function} dispatch - Redux action dispatcher.
  342. * @returns {{
  343. * _onRoomLock: Function,
  344. * _onToggleAudioOnly: Function,
  345. * _onToggleCameraFacingMode: Function,
  346. * }}
  347. * @private
  348. */
  349. function _mapDispatchToProps(dispatch) {
  350. return {
  351. ...abstractMapDispatchToProps(dispatch),
  352. /**
  353. * Sets the lock i.e. password protection of the conference/room.
  354. *
  355. * @private
  356. * @returns {void}
  357. * @type {Function}
  358. */
  359. _onRoomLock() {
  360. dispatch(beginRoomLockRequest());
  361. },
  362. /**
  363. * Begins the UI procedure to share the conference/room URL.
  364. *
  365. * @private
  366. * @returns {void}
  367. * @type {Function}
  368. */
  369. _onShareRoom() {
  370. dispatch(beginShareRoom());
  371. },
  372. /**
  373. * Toggles the audio-only flag of the conference.
  374. *
  375. * @private
  376. * @returns {void}
  377. * @type {Function}
  378. */
  379. _onToggleAudioOnly() {
  380. dispatch(toggleAudioOnly());
  381. },
  382. /**
  383. * Switches between the front/user-facing and back/environment-facing
  384. * cameras.
  385. *
  386. * @private
  387. * @returns {void}
  388. * @type {Function}
  389. */
  390. _onToggleCameraFacingMode() {
  391. dispatch(toggleCameraFacingMode());
  392. }
  393. };
  394. }
  395. /**
  396. * Maps part of Redux store to React component props.
  397. *
  398. * @param {Object} state - Redux store.
  399. * @returns {{
  400. * _audioOnly: boolean,
  401. * _locked: boolean
  402. * }}
  403. * @private
  404. */
  405. function _mapStateToProps(state) {
  406. const conference = state['features/base/conference'];
  407. const { enabled } = state['features/toolbox'];
  408. return {
  409. ...abstractMapStateToProps(state),
  410. /**
  411. * The indicator which determines whether the conference is in
  412. * audio-only mode.
  413. *
  414. * @protected
  415. * @type {boolean}
  416. */
  417. _audioOnly: Boolean(conference.audioOnly),
  418. /**
  419. * The indicator which determines whether the toolbox is enabled.
  420. *
  421. * @private
  422. * @type {boolean}
  423. */
  424. _enabled: enabled,
  425. /**
  426. * The indicator which determines whether the conference is
  427. * locked/password-protected.
  428. *
  429. * @protected
  430. * @type {boolean}
  431. */
  432. _locked: Boolean(conference.locked)
  433. };
  434. }
  435. export default connect(_mapStateToProps, _mapDispatchToProps)(
  436. makeAspectRatioAware(Toolbox));