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

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