您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CallOverlay.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { Audio } from '../../base/media';
  5. import { Avatar } from '../../base/participants';
  6. import { Container, Text } from '../../base/react';
  7. import UIEvents from '../../../../service/UI/UIEvents';
  8. declare var $: Object;
  9. declare var APP: Object;
  10. declare var interfaceConfig: Object;
  11. /**
  12. * Implements a React {@link Component} which depicts the establishment of a
  13. * call with a specific remote callee.
  14. *
  15. * @extends Component
  16. */
  17. class CallOverlay extends Component {
  18. /**
  19. * The (reference to the) {@link Audio} which plays/renders the audio
  20. * depicting the ringing phase of the call establishment represented by this
  21. * {@code CallOverlay}.
  22. */
  23. _audio: ?Audio
  24. _onLargeVideoAvatarVisible: Function
  25. _playAudioInterval: ?number
  26. _ringingTimeout: ?number
  27. _setAudio: Function
  28. state: {
  29. /**
  30. * The CSS class (name), if any, to add to this {@code CallOverlay}.
  31. *
  32. * @type {string}
  33. */
  34. className: ?string,
  35. /**
  36. * The indicator which determines whether this {@code CallOverlay}
  37. * should play/render audio to indicate the ringing phase of the
  38. * call establishment between the local participant and the
  39. * associated remote callee.
  40. *
  41. * @type {boolean}
  42. */
  43. renderAudio: boolean,
  44. /**
  45. * The indicator which determines whether this {@code CallOverlay}
  46. * is depicting the ringing phase of the call establishment between
  47. * the local participant and the associated remote callee or the
  48. * phase afterwards when the callee has not answered the call for a
  49. * period of time and, consequently, is considered unavailable.
  50. *
  51. * @type {boolean}
  52. */
  53. ringing: boolean
  54. }
  55. /**
  56. * {@code CallOverlay} component's property types.
  57. *
  58. * @static
  59. */
  60. static propTypes = {
  61. _callee: React.PropTypes.object
  62. };
  63. /**
  64. * Initializes a new {@code CallOverlay} instance.
  65. *
  66. * @param {Object} props - The read-only React {@link Component} props with
  67. * which the new instance is to be initialized.
  68. */
  69. constructor(props) {
  70. super(props);
  71. this.state = {
  72. className: undefined,
  73. renderAudio:
  74. typeof interfaceConfig !== 'object'
  75. || !interfaceConfig.DISABLE_RINGING,
  76. ringing: true
  77. };
  78. this._onLargeVideoAvatarVisible
  79. = this._onLargeVideoAvatarVisible.bind(this);
  80. this._setAudio = this._setAudio.bind(this);
  81. if (typeof APP === 'object') {
  82. APP.UI.addListener(
  83. UIEvents.LARGE_VIDEO_AVATAR_VISIBLE,
  84. this._onLargeVideoAvatarVisible);
  85. }
  86. }
  87. /**
  88. * Sets up timeouts such as the timeout to end the ringing phase of the call
  89. * establishment depicted by this {@code CallOverlay}.
  90. *
  91. * @inheritdoc
  92. */
  93. componentDidMount() {
  94. // Set up the timeout to end the ringing phase of the call establishment
  95. // depicted by this CallOverlay.
  96. if (this.state.ringing && !this._ringingTimeout) {
  97. this._ringingTimeout
  98. = setTimeout(
  99. () => {
  100. this._pauseAudio();
  101. this._ringingTimeout = undefined;
  102. this.setState({
  103. ringing: false
  104. });
  105. },
  106. 30000);
  107. }
  108. this._playAudio();
  109. }
  110. /**
  111. * Cleans up before this {@code Calleverlay} is unmounted and destroyed.
  112. *
  113. * @inheritdoc
  114. */
  115. componentWillUnmount() {
  116. this._pauseAudio();
  117. if (this._ringingTimeout) {
  118. clearTimeout(this._ringingTimeout);
  119. this._ringingTimeout = undefined;
  120. }
  121. if (typeof APP === 'object') {
  122. APP.UI.removeListener(
  123. UIEvents.LARGE_VIDEO_AVATAR_VISIBLE,
  124. this._onLargeVideoAvatarVisible);
  125. }
  126. }
  127. /**
  128. * Implements React's {@link Component#render()}.
  129. *
  130. * @inheritdoc
  131. * @returns {ReactElement}
  132. */
  133. render() {
  134. const { className, ringing } = this.state;
  135. const { avatarUrl, name } = this.props._callee;
  136. return (
  137. <Container
  138. className = { `ringing ${className || ''}` }
  139. id = 'ringOverlay'>
  140. <Container className = 'ringing__content'>
  141. { ringing ? <Text>Calling...</Text> : null }
  142. <Avatar
  143. className = 'ringing__avatar'
  144. uri = { avatarUrl } />
  145. <Container className = 'ringing__caller-info'>
  146. <Text>
  147. { name }
  148. { ringing ? null : ' isn\'t available' }
  149. </Text>
  150. </Container>
  151. </Container>
  152. { this._renderAudio() }
  153. </Container>
  154. );
  155. }
  156. /**
  157. * Notifies this {@code CallOverlay} that the visibility of the
  158. * participant's avatar in the large video has changed.
  159. *
  160. * @param {boolean} largeVideoAvatarVisible - If the avatar in the large
  161. * video (i.e. of the participant on the stage) is visible, then
  162. * {@code true}; otherwise, {@code false}.
  163. * @private
  164. * @returns {void}
  165. */
  166. _onLargeVideoAvatarVisible(largeVideoAvatarVisible: boolean) {
  167. this.setState({
  168. className: largeVideoAvatarVisible ? 'solidBG' : undefined
  169. });
  170. }
  171. /**
  172. * Stops the playback of the audio which represents the ringing phase of the
  173. * call establishment depicted by this {@code CallOverlay}.
  174. *
  175. * @private
  176. * @returns {void}
  177. */
  178. _pauseAudio() {
  179. const audio = this._audio;
  180. if (audio) {
  181. audio.pause();
  182. }
  183. if (this._playAudioInterval) {
  184. clearInterval(this._playAudioInterval);
  185. this._playAudioInterval = undefined;
  186. }
  187. }
  188. /**
  189. * Starts the playback of the audio which represents the ringing phase of
  190. * the call establishment depicted by this {@code CallOverlay}.
  191. *
  192. * @private
  193. * @returns {void}
  194. */
  195. _playAudio() {
  196. if (this._audio) {
  197. this._audio.play();
  198. if (!this._playAudioInterval) {
  199. this._playAudioInterval
  200. = setInterval(() => this._playAudio(), 5000);
  201. }
  202. }
  203. }
  204. /**
  205. * Renders an audio element to represent the ringing phase of the call
  206. * establishment represented by this {@code CallOverlay}.
  207. *
  208. * @private
  209. * @returns {ReactElement}
  210. */
  211. _renderAudio() {
  212. if (this.state.renderAudio && this.state.ringing) {
  213. return (
  214. <Audio
  215. ref = { this._setAudio }
  216. src = './sounds/ring.ogg' />
  217. );
  218. }
  219. return null;
  220. }
  221. /**
  222. * Sets the (reference to the) {@link Audio} which renders the ringing phase
  223. * of the call establishment represented by this {@code CallOverlay}.
  224. *
  225. * @param {Audio} audio - The (reference to the) {@code Audio} which
  226. * plays/renders the audio depicting the ringing phase of the call
  227. * establishment represented by this {@code CallOverlay}.
  228. * @private
  229. * @returns {void}
  230. */
  231. _setAudio(audio) {
  232. this._audio = audio;
  233. }
  234. }
  235. /**
  236. * Maps (parts of) the redux state to {@code CallOverlay}'s props.
  237. *
  238. * @param {Object} state - The redux state.
  239. * @private
  240. * @returns {{
  241. * _callee: Object
  242. * }}
  243. */
  244. function _mapStateToProps(state) {
  245. return {
  246. /**
  247. *
  248. * @private
  249. * @type {Object}
  250. */
  251. _callee: state['features/jwt'].callee
  252. };
  253. }
  254. export default connect(_mapStateToProps)(CallOverlay);