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

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