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

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