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

YoutubeLargeVideo.js 8.9KB

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