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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // @flow
  2. import React, { Component, createRef } 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';
  8. import { setToolboxVisible } from '../../../toolbox/actions';
  9. import { setSharedVideoStatus } from '../../actions.native';
  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. * The id of the participant sharing the video.
  53. *
  54. * @private
  55. */
  56. _ownerId: string,
  57. /**
  58. * The height of the player.
  59. *
  60. * @private
  61. */
  62. _playerHeight: number,
  63. /**
  64. * The width of the player.
  65. *
  66. * @private
  67. */
  68. _playerWidth: number,
  69. /**
  70. * Seek time in seconds.
  71. *
  72. * @private
  73. */
  74. _seek: number,
  75. /**
  76. * The status of the player.
  77. *
  78. * @private
  79. */
  80. _status: string,
  81. /**
  82. * The Redux dispatch function.
  83. */
  84. dispatch: Function,
  85. /**
  86. * Youtube id of the video to be played.
  87. *
  88. * @private
  89. */
  90. youtubeId: string
  91. };
  92. /**
  93. *
  94. * Implements a React {@code Component} for showing a youtube video.
  95. *
  96. * @extends Component
  97. */
  98. class YoutubeLargeVideo extends Component<Props, *> {
  99. /**
  100. * Saves a handle to the timer for seek time updates,
  101. * so that it can be cancelled when the component unmounts.
  102. */
  103. intervalId: ?IntervalID;
  104. /**
  105. * A React ref to the HTML element containing the {@code YoutubePlayer} instance.
  106. */
  107. playerRef: Object;
  108. /**
  109. * Initializes a new {@code YoutubeLargeVideo} instance.
  110. *
  111. * @param {Object} props - The read-only properties with which the new
  112. * instance is to be initialized.
  113. */
  114. constructor(props: Props) {
  115. super(props);
  116. this.playerRef = createRef();
  117. this._onReady = this._onReady.bind(this);
  118. this._onChangeState = this._onChangeState.bind(this);
  119. this.setWideScreenMode(props._isWideScreen);
  120. }
  121. /**
  122. * Seeks to the new time if the difference between the new one and the current is larger than 5 seconds.
  123. *
  124. * @inheritdoc
  125. * @returns {void}
  126. */
  127. componentDidUpdate(prevProps: Props) {
  128. const playerRef = this.playerRef.current;
  129. const { _isWideScreen, _seek } = this.props;
  130. if (_seek !== prevProps._seek) {
  131. playerRef && playerRef.getCurrentTime().then(time => {
  132. if (shouldSeekToPosition(_seek, time)) {
  133. playerRef && playerRef.seekTo(_seek);
  134. }
  135. });
  136. }
  137. if (_isWideScreen !== prevProps._isWideScreen) {
  138. this.setWideScreenMode(_isWideScreen);
  139. }
  140. }
  141. /**
  142. * Sets the interval for saving the seek time to redux every 5 seconds.
  143. *
  144. * @inheritdoc
  145. * @returns {void}
  146. */
  147. componentDidMount() {
  148. this.intervalId = setInterval(() => {
  149. this.saveRefTime();
  150. }, 5000);
  151. }
  152. /**
  153. * Clears the interval.
  154. *
  155. * @inheritdoc
  156. * @returns {void}
  157. */
  158. componentWillUnmount() {
  159. clearInterval(this.intervalId);
  160. this.intervalId = null;
  161. }
  162. /**
  163. * Renders the YoutubeLargeVideo element.
  164. *
  165. * @override
  166. * @returns {ReactElement}
  167. */
  168. render() {
  169. const {
  170. _enableControls,
  171. _isPlaying,
  172. _playerHeight,
  173. _playerWidth,
  174. youtubeId
  175. } = this.props;
  176. return (
  177. <View
  178. pointerEvents = { _enableControls ? 'auto' : 'none' }
  179. style = { styles.youtubeVideoContainer } >
  180. <YoutubePlayer
  181. height = { _playerHeight }
  182. initialPlayerParams = {{
  183. controls: _enableControls,
  184. modestbranding: true,
  185. preventFullScreen: true
  186. }}
  187. /* eslint-disable react/jsx-no-bind */
  188. onChangeState = { this._onChangeState }
  189. /* eslint-disable react/jsx-no-bind */
  190. onReady = { this._onReady }
  191. play = { _isPlaying }
  192. playbackRate = { 1 }
  193. ref = { this.playerRef }
  194. videoId = { youtubeId }
  195. volume = { 50 }
  196. webViewProps = {{
  197. bounces: false,
  198. mediaPlaybackRequiresUserAction: false,
  199. scrollEnabled: false,
  200. userAgent: webviewUserAgent
  201. }}
  202. width = { _playerWidth } />
  203. </View>);
  204. }
  205. _onReady: () => void;
  206. /**
  207. * Callback invoked when the player is ready to play the video.
  208. *
  209. * @private
  210. * @returns {void}
  211. */
  212. _onReady() {
  213. if (this.props?._isOwner) {
  214. this.onVideoReady(
  215. this.props.youtubeId,
  216. this.playerRef.current && this.playerRef.current.getCurrentTime(),
  217. this.props._ownerId);
  218. }
  219. }
  220. _onChangeState: (status: string) => void;
  221. /**
  222. * Callback invoked when the state of the player changes.
  223. *
  224. * @param {string} status - The new status of the player.
  225. * @private
  226. * @returns {void}
  227. */
  228. _onChangeState(status) {
  229. this.playerRef?.current && this.playerRef.current.getCurrentTime().then(time => {
  230. const {
  231. _isOwner,
  232. _isPlaying,
  233. _isStopped,
  234. _ownerId,
  235. _seek,
  236. youtubeId
  237. } = this.props;
  238. if (shouldSetNewStatus(_isStopped, _isOwner, status, _isPlaying, time, _seek)) {
  239. this.onVideoChangeEvent(youtubeId, status, time, _ownerId);
  240. }
  241. });
  242. }
  243. /**
  244. * Calls onVideoChangeEvent with the refTime.
  245. *
  246. * @private
  247. * @returns {void}
  248. */
  249. saveRefTime() {
  250. const { youtubeId, _status, _ownerId } = this.props;
  251. this.playerRef.current && this.playerRef.current.getCurrentTime().then(time => {
  252. this.onVideoChangeEvent(youtubeId, _status, time, _ownerId);
  253. });
  254. }
  255. /**
  256. * Dispatches the video status, time and ownerId if the status is playing or paused.
  257. *
  258. * @param {string} videoUrl - The youtube id of the video.
  259. * @param {string} status - The status of the player.
  260. * @param {number} time - The seek time.
  261. * @param {string} ownerId - The id of the participant sharing the video.
  262. * @private
  263. * @returns {void}
  264. */
  265. onVideoChangeEvent(videoUrl, status, time, ownerId) {
  266. if (![ 'playing', 'paused' ].includes(status)) {
  267. return;
  268. }
  269. this.props.dispatch(setSharedVideoStatus({
  270. videoUrl,
  271. status: translateStatus(status),
  272. time,
  273. ownerId
  274. }));
  275. }
  276. /**
  277. * Dispatches the 'playing' as video status, time and ownerId.
  278. *
  279. * @param {string} videoUrl - The youtube id of the video.
  280. * @param {number} time - The seek time.
  281. * @param {string} ownerId - The id of the participant sharing the video.
  282. * @private
  283. * @returns {void}
  284. */
  285. onVideoReady(videoUrl, time, ownerId) {
  286. time.then(t => this.props.dispatch(setSharedVideoStatus({
  287. videoUrl,
  288. status: 'playing',
  289. time: t,
  290. ownerId
  291. })));
  292. }
  293. /**
  294. * Dispatches action to set the visibility of the toolbox, true if not widescreen, false otherwise.
  295. *
  296. * @param {isWideScreen} isWideScreen - Whether the screen is wide.
  297. * @private
  298. * @returns {void}
  299. */
  300. setWideScreenMode(isWideScreen) {
  301. this.props.dispatch(setToolboxVisible(!isWideScreen));
  302. }
  303. }
  304. /* eslint-disable max-params */
  305. /**
  306. * Return true if the user is the owner and
  307. * the status has changed or the seek time difference from the previous set is larger than 5 seconds.
  308. *
  309. * @param {boolean} isStopped - Once the status was set to stop, all the other statuses should be ignored.
  310. * @param {boolean} isOwner - Whether the local user is sharing the video.
  311. * @param {string} status - The new status.
  312. * @param {boolean} isPlaying - Whether the component is playing at the moment.
  313. * @param {number} newTime - The new seek time.
  314. * @param {number} previousTime - The old seek time.
  315. * @private
  316. * @returns {boolean}
  317. */
  318. function shouldSetNewStatus(isStopped, isOwner, status, isPlaying, newTime, previousTime) {
  319. if (isStopped) {
  320. return false;
  321. }
  322. if (!isOwner || status === 'buffering') {
  323. return false;
  324. }
  325. if ((isPlaying && status === 'paused') || (!isPlaying && status === 'playing')) {
  326. return true;
  327. }
  328. return shouldSeekToPosition(newTime, previousTime);
  329. }
  330. /**
  331. * Return true if the diffenrece between the two timees is larger than 5.
  332. *
  333. * @param {number} newTime - The current time.
  334. * @param {number} previousTime - The previous time.
  335. * @private
  336. * @returns {boolean}
  337. */
  338. function shouldSeekToPosition(newTime, previousTime) {
  339. return Math.abs(newTime - previousTime) > 5;
  340. }
  341. /**
  342. * Maps (parts of) the Redux state to the associated YoutubeLargeVideo's props.
  343. *
  344. * @param {Object} state - Redux state.
  345. * @private
  346. * @returns {Props}
  347. */
  348. function _mapStateToProps(state) {
  349. const { ownerId, status, time } = state['features/shared-video'];
  350. const localParticipant = getLocalParticipant(state);
  351. const responsiveUi = state['features/base/responsive-ui'];
  352. const { aspectRatio, clientHeight: screenHeight, clientWidth: screenWidth } = responsiveUi;
  353. const isWideScreen = aspectRatio === ASPECT_RATIO_WIDE;
  354. let playerHeight, playerWidth;
  355. if (isWideScreen) {
  356. playerHeight = screenHeight;
  357. playerWidth = playerHeight * 16 / 9;
  358. } else {
  359. playerWidth = screenWidth;
  360. playerHeight = playerWidth * 9 / 16;
  361. }
  362. return {
  363. _enableControls: ownerId === localParticipant.id,
  364. _isOwner: ownerId === localParticipant.id,
  365. _isPlaying: status === 'playing',
  366. _isStopped: status === 'stop',
  367. _isWideScreen: isWideScreen,
  368. _ownerId: ownerId,
  369. _playerHeight: playerHeight,
  370. _playerWidth: playerWidth,
  371. _seek: time,
  372. _status: status
  373. };
  374. }
  375. /**
  376. * In case the status is 'paused', it is translated to 'pause' to match the web functionality.
  377. *
  378. * @param {string} status - The status of the shared video.
  379. * @private
  380. * @returns {string}
  381. */
  382. function translateStatus(status) {
  383. if (status === 'paused') {
  384. return 'pause';
  385. }
  386. return status;
  387. }
  388. export default connect(_mapStateToProps)(YoutubeLargeVideo);