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.

YoutubeLargeVideo.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // @flow
  2. import React, { useRef, useEffect } from 'react';
  3. import { View } from 'react-native';
  4. import YoutubePlayer from 'react-native-youtube-iframe';
  5. import { getLocalParticipant } from '../../../base/participants';
  6. import { connect } from '../../../base/redux';
  7. import { ASPECT_RATIO_WIDE } from '../../../base/responsive-ui/constants';
  8. import { setToolboxVisible } from '../../../toolbox/actions';
  9. import { setSharedVideoStatus } from '../../actions';
  10. import styles from './styles';
  11. /**
  12. * Passed to the webviewProps in order to avoid the usage of the ios player on which we cannot hide the controls.
  13. *
  14. * @private
  15. */
  16. const webviewUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'; // eslint-disable-line max-len
  17. /**
  18. * The type of the React {@link Component} props of {@link YoutubeLargeVideo}.
  19. */
  20. type Props = {
  21. /**
  22. * Display the youtube controls on the player.
  23. *
  24. * @private
  25. */
  26. _enableControls: boolean,
  27. /**
  28. * Is the video shared by the local user.
  29. *
  30. * @private
  31. */
  32. _isOwner: boolean,
  33. /**
  34. * The ID of the participant (to be) depicted by LargeVideo.
  35. *
  36. * @private
  37. */
  38. _isPlaying: string,
  39. /**
  40. * True if in landscape mode.
  41. *
  42. * @private
  43. */
  44. _isWideScreen: boolean,
  45. /**
  46. * Callback to invoke when the {@code YoutLargeVideo} is ready to play.
  47. *
  48. * @private
  49. */
  50. _onVideoReady: Function,
  51. /**
  52. * Callback to invoke when the {@code YoutubeLargeVideo} changes status.
  53. *
  54. * @private
  55. */
  56. _onVideoChangeEvent: Function,
  57. /**
  58. * Callback to invoke when { @code isWideScreen} changes.
  59. *
  60. * @private
  61. */
  62. _onWideScreenChanged: Function,
  63. /**
  64. * The id of the participant sharing the video.
  65. *
  66. * @private
  67. */
  68. _ownerId: string,
  69. /**
  70. * The height of the screen.
  71. *
  72. * @private
  73. */
  74. _screenHeight: number,
  75. /**
  76. * The width of the screen.
  77. *
  78. * @private
  79. */
  80. _screenWidth: number,
  81. /**
  82. * Seek time in seconds.
  83. *
  84. * @private
  85. */
  86. _seek: number,
  87. /**
  88. * Youtube id of the video to be played.
  89. *
  90. * @private
  91. */
  92. youtubeId: string
  93. };
  94. const YoutubeLargeVideo = (props: Props) => {
  95. const playerRef = useRef(null);
  96. useEffect(() => {
  97. playerRef.current && playerRef.current.getCurrentTime().then(time => {
  98. const { _seek } = props;
  99. if (shouldSeekToPosition(_seek, time)) {
  100. playerRef.current && playerRef.current.seekTo(_seek);
  101. }
  102. });
  103. }, [ props._seek ]);
  104. useEffect(() => {
  105. props._onWideScreenChanged(props._isWideScreen);
  106. }, [ props._isWideScreen ]);
  107. const onChangeState = e =>
  108. playerRef.current && playerRef.current.getCurrentTime().then(time => {
  109. const {
  110. _isOwner,
  111. _isPlaying,
  112. _seek
  113. } = props;
  114. if (shouldSetNewStatus(_isOwner, e, _isPlaying, time, _seek)) {
  115. props._onVideoChangeEvent(props.youtubeId, e, time, props._ownerId);
  116. }
  117. });
  118. const onReady = () => {
  119. if (props._isOwner) {
  120. props._onVideoReady(
  121. props.youtubeId,
  122. playerRef.current && playerRef.current.getCurrentTime(),
  123. props._ownerId);
  124. }
  125. };
  126. let playerHeight, playerWidth;
  127. if (props._isWideScreen) {
  128. playerHeight = props._screenHeight;
  129. playerWidth = playerHeight * 16 / 9;
  130. } else {
  131. playerWidth = props._screenWidth;
  132. playerHeight = playerWidth * 9 / 16;
  133. }
  134. return (
  135. <View
  136. pointerEvents = { props._enableControls ? 'auto' : 'none' }
  137. style = { styles.youtubeVideoContainer } >
  138. <YoutubePlayer
  139. height = { playerHeight }
  140. initialPlayerParams = {{
  141. controls: props._enableControls,
  142. modestbranding: true,
  143. preventFullScreen: true
  144. }}
  145. /* eslint-disable react/jsx-no-bind */
  146. onChangeState = { onChangeState }
  147. /* eslint-disable react/jsx-no-bind */
  148. onReady = { onReady }
  149. play = { props._isPlaying }
  150. playbackRate = { 1 }
  151. ref = { playerRef }
  152. videoId = { props.youtubeId }
  153. volume = { 50 }
  154. webViewProps = {{
  155. bounces: false,
  156. mediaPlaybackRequiresUserAction: false,
  157. scrollEnabled: false,
  158. userAgent: webviewUserAgent
  159. }}
  160. width = { playerWidth } />
  161. </View>);
  162. };
  163. /* eslint-disable max-params */
  164. /**
  165. * Return true if the user is the owner and
  166. * the status has changed or the seek time difference from the previous set is larger than 5 seconds.
  167. *
  168. * @param {boolean} isOwner - Whether the local user is sharing the video.
  169. * @param {string} status - The new status.
  170. * @param {boolean} isPlaying - Whether the component is playing at the moment.
  171. * @param {number} newTime - The new seek time.
  172. * @param {number} previousTime - The old seek time.
  173. * @private
  174. * @returns {boolean}
  175. */
  176. function shouldSetNewStatus(isOwner, status, isPlaying, newTime, previousTime) {
  177. if (!isOwner || status === 'buffering') {
  178. return false;
  179. }
  180. if ((isPlaying && status === 'paused') || (!isPlaying && status === 'playing')) {
  181. return true;
  182. }
  183. return shouldSeekToPosition(newTime, previousTime);
  184. }
  185. /**
  186. * Return true if the diffenrece between the two timees is larger than 5.
  187. *
  188. * @param {number} newTime - The current time.
  189. * @param {number} previousTime - The previous time.
  190. * @private
  191. * @returns {boolean}
  192. */
  193. function shouldSeekToPosition(newTime, previousTime) {
  194. return Math.abs(newTime - previousTime) > 5;
  195. }
  196. /**
  197. * Maps (parts of) the Redux state to the associated YoutubeLargeVideo's props.
  198. *
  199. * @param {Object} state - Redux state.
  200. * @private
  201. * @returns {Props}
  202. */
  203. function _mapStateToProps(state) {
  204. const { ownerId, status, time } = state['features/youtube-player'];
  205. const localParticipant = getLocalParticipant(state);
  206. const responsiveUi = state['features/base/responsive-ui'];
  207. const screenHeight = responsiveUi.clientHeight;
  208. const screenWidth = responsiveUi.clientWidth;
  209. return {
  210. _enableControls: ownerId === localParticipant.id,
  211. _isOwner: ownerId === localParticipant.id,
  212. _isPlaying: status === 'playing',
  213. _isWideScreen: responsiveUi.aspectRatio === ASPECT_RATIO_WIDE,
  214. _ownerId: ownerId,
  215. _screenHeight: screenHeight,
  216. _screenWidth: screenWidth,
  217. _seek: time
  218. };
  219. }
  220. /**
  221. * Maps dispatching of some action to React component props.
  222. *
  223. * @param {Function} dispatch - Redux action dispatcher.
  224. * @private
  225. * @returns {{
  226. * onVideoChangeEvent: Function,
  227. * onVideoReady: Function,
  228. * onWideScreenChanged: Function
  229. * }}
  230. */
  231. function _mapDispatchToProps(dispatch) {
  232. return {
  233. _onVideoChangeEvent: (videoId, status, time, ownerId) => {
  234. if (![ 'playing', 'paused' ].includes(status)) {
  235. return;
  236. }
  237. dispatch(setSharedVideoStatus(videoId, translateStatus(status), time, ownerId));
  238. },
  239. _onVideoReady: (videoId, time, ownerId) => {
  240. time.then(t => dispatch(setSharedVideoStatus(videoId, 'playing', t, ownerId)));
  241. },
  242. _onWideScreenChanged: isWideScreen => {
  243. dispatch(setToolboxVisible(!isWideScreen));
  244. }
  245. };
  246. }
  247. /**
  248. * Maps (parts of) the Redux state to the associated YoutubeLargeVideo's props.
  249. *
  250. * @private
  251. * @returns {Props}
  252. */
  253. function _mergeProps({ _isOwner, ...stateProps }, { _onVideoChangeEvent, _onVideoReady, _onWideScreenChanged }) {
  254. return Object.assign(stateProps, {
  255. _onVideoChangeEvent: _isOwner ? _onVideoChangeEvent : () => { /* do nothing */ },
  256. _onVideoReady: _isOwner ? _onVideoReady : () => { /* do nothing */ },
  257. _onWideScreenChanged
  258. });
  259. }
  260. /**
  261. * In case the status is 'paused', it is translated to 'pause' to match the web functionality.
  262. *
  263. * @param {string} status - The status of the shared video.
  264. * @private
  265. * @returns {string}
  266. */
  267. function translateStatus(status) {
  268. if (status === 'paused') {
  269. return 'pause';
  270. }
  271. return status;
  272. }
  273. export default connect(_mapStateToProps, _mapDispatchToProps, _mergeProps)(YoutubeLargeVideo);