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.

664 lines
20 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  4. import { fromJS, is } from 'immutable';
  5. import { throttle, debounce } from 'lodash';
  6. import classNames from 'classnames';
  7. import { isFullscreen, requestFullscreen, exitFullscreen } from '../ui/util/fullscreen';
  8. import { displayMedia, useBlurhash } from '../../initial_state';
  9. import Icon from 'mastodon/components/icon';
  10. import Blurhash from 'mastodon/components/blurhash';
  11. const messages = defineMessages({
  12. play: { id: 'video.play', defaultMessage: 'Play' },
  13. pause: { id: 'video.pause', defaultMessage: 'Pause' },
  14. mute: { id: 'video.mute', defaultMessage: 'Mute sound' },
  15. unmute: { id: 'video.unmute', defaultMessage: 'Unmute sound' },
  16. hide: { id: 'video.hide', defaultMessage: 'Hide video' },
  17. expand: { id: 'video.expand', defaultMessage: 'Expand video' },
  18. close: { id: 'video.close', defaultMessage: 'Close video' },
  19. fullscreen: { id: 'video.fullscreen', defaultMessage: 'Full screen' },
  20. exit_fullscreen: { id: 'video.exit_fullscreen', defaultMessage: 'Exit full screen' },
  21. });
  22. export const formatTime = secondsNum => {
  23. let hours = Math.floor(secondsNum / 3600);
  24. let minutes = Math.floor((secondsNum - (hours * 3600)) / 60);
  25. let seconds = secondsNum - (hours * 3600) - (minutes * 60);
  26. if (hours < 10) hours = '0' + hours;
  27. if (minutes < 10) minutes = '0' + minutes;
  28. if (seconds < 10) seconds = '0' + seconds;
  29. return (hours === '00' ? '' : `${hours}:`) + `${minutes}:${seconds}`;
  30. };
  31. export const findElementPosition = el => {
  32. let box;
  33. if (el.getBoundingClientRect && el.parentNode) {
  34. box = el.getBoundingClientRect();
  35. }
  36. if (!box) {
  37. return {
  38. left: 0,
  39. top: 0,
  40. };
  41. }
  42. const docEl = document.documentElement;
  43. const body = document.body;
  44. const clientLeft = docEl.clientLeft || body.clientLeft || 0;
  45. const scrollLeft = window.pageXOffset || body.scrollLeft;
  46. const left = (box.left + scrollLeft) - clientLeft;
  47. const clientTop = docEl.clientTop || body.clientTop || 0;
  48. const scrollTop = window.pageYOffset || body.scrollTop;
  49. const top = (box.top + scrollTop) - clientTop;
  50. return {
  51. left: Math.round(left),
  52. top: Math.round(top),
  53. };
  54. };
  55. export const getPointerPosition = (el, event) => {
  56. const position = {};
  57. const box = findElementPosition(el);
  58. const boxW = el.offsetWidth;
  59. const boxH = el.offsetHeight;
  60. const boxY = box.top;
  61. const boxX = box.left;
  62. let pageY = event.pageY;
  63. let pageX = event.pageX;
  64. if (event.changedTouches) {
  65. pageX = event.changedTouches[0].pageX;
  66. pageY = event.changedTouches[0].pageY;
  67. }
  68. position.y = Math.max(0, Math.min(1, (pageY - boxY) / boxH));
  69. position.x = Math.max(0, Math.min(1, (pageX - boxX) / boxW));
  70. return position;
  71. };
  72. export const fileNameFromURL = str => {
  73. const url = new URL(str);
  74. const pathname = url.pathname;
  75. const index = pathname.lastIndexOf('/');
  76. return pathname.substring(index + 1);
  77. };
  78. export default @injectIntl
  79. class Video extends React.PureComponent {
  80. static propTypes = {
  81. preview: PropTypes.string,
  82. frameRate: PropTypes.string,
  83. src: PropTypes.string.isRequired,
  84. alt: PropTypes.string,
  85. width: PropTypes.number,
  86. height: PropTypes.number,
  87. sensitive: PropTypes.bool,
  88. currentTime: PropTypes.number,
  89. onOpenVideo: PropTypes.func,
  90. onCloseVideo: PropTypes.func,
  91. detailed: PropTypes.bool,
  92. inline: PropTypes.bool,
  93. editable: PropTypes.bool,
  94. alwaysVisible: PropTypes.bool,
  95. cacheWidth: PropTypes.func,
  96. visible: PropTypes.bool,
  97. onToggleVisibility: PropTypes.func,
  98. deployPictureInPicture: PropTypes.func,
  99. intl: PropTypes.object.isRequired,
  100. blurhash: PropTypes.string,
  101. autoPlay: PropTypes.bool,
  102. volume: PropTypes.number,
  103. muted: PropTypes.bool,
  104. };
  105. static defaultProps = {
  106. frameRate: 25,
  107. };
  108. state = {
  109. currentTime: 0,
  110. duration: 0,
  111. volume: 0.5,
  112. paused: true,
  113. dragging: false,
  114. containerWidth: this.props.width,
  115. fullscreen: false,
  116. hovered: false,
  117. muted: false,
  118. revealed: this.props.visible !== undefined ? this.props.visible : (displayMedia !== 'hide_all' && !this.props.sensitive || displayMedia === 'show_all'),
  119. };
  120. setPlayerRef = c => {
  121. this.player = c;
  122. if (this.player) {
  123. this._setDimensions();
  124. }
  125. }
  126. _setDimensions () {
  127. const width = this.player.offsetWidth;
  128. if (this.props.cacheWidth) {
  129. this.props.cacheWidth(width);
  130. }
  131. this.setState({
  132. containerWidth: width,
  133. });
  134. }
  135. setVideoRef = c => {
  136. this.video = c;
  137. if (this.video) {
  138. this.setState({ volume: this.video.volume, muted: this.video.muted });
  139. }
  140. }
  141. setSeekRef = c => {
  142. this.seek = c;
  143. }
  144. setVolumeRef = c => {
  145. this.volume = c;
  146. }
  147. handleClickRoot = e => e.stopPropagation();
  148. handlePlay = () => {
  149. this.setState({ paused: false });
  150. this._updateTime();
  151. }
  152. handlePause = () => {
  153. this.setState({ paused: true });
  154. }
  155. _updateTime () {
  156. requestAnimationFrame(() => {
  157. if (!this.video) return;
  158. this.handleTimeUpdate();
  159. if (!this.state.paused) {
  160. this._updateTime();
  161. }
  162. });
  163. }
  164. handleTimeUpdate = () => {
  165. this.setState({
  166. currentTime: this.video.currentTime,
  167. duration:this.video.duration,
  168. });
  169. }
  170. handleVolumeMouseDown = e => {
  171. document.addEventListener('mousemove', this.handleMouseVolSlide, true);
  172. document.addEventListener('mouseup', this.handleVolumeMouseUp, true);
  173. document.addEventListener('touchmove', this.handleMouseVolSlide, true);
  174. document.addEventListener('touchend', this.handleVolumeMouseUp, true);
  175. this.handleMouseVolSlide(e);
  176. e.preventDefault();
  177. e.stopPropagation();
  178. }
  179. handleVolumeMouseUp = () => {
  180. document.removeEventListener('mousemove', this.handleMouseVolSlide, true);
  181. document.removeEventListener('mouseup', this.handleVolumeMouseUp, true);
  182. document.removeEventListener('touchmove', this.handleMouseVolSlide, true);
  183. document.removeEventListener('touchend', this.handleVolumeMouseUp, true);
  184. }
  185. handleMouseVolSlide = throttle(e => {
  186. const { x } = getPointerPosition(this.volume, e);
  187. if(!isNaN(x)) {
  188. this.setState({ volume: x }, () => {
  189. this.video.volume = x;
  190. });
  191. }
  192. }, 15);
  193. handleMouseDown = e => {
  194. document.addEventListener('mousemove', this.handleMouseMove, true);
  195. document.addEventListener('mouseup', this.handleMouseUp, true);
  196. document.addEventListener('touchmove', this.handleMouseMove, true);
  197. document.addEventListener('touchend', this.handleMouseUp, true);
  198. this.setState({ dragging: true });
  199. this.video.pause();
  200. this.handleMouseMove(e);
  201. e.preventDefault();
  202. e.stopPropagation();
  203. }
  204. handleMouseUp = () => {
  205. document.removeEventListener('mousemove', this.handleMouseMove, true);
  206. document.removeEventListener('mouseup', this.handleMouseUp, true);
  207. document.removeEventListener('touchmove', this.handleMouseMove, true);
  208. document.removeEventListener('touchend', this.handleMouseUp, true);
  209. this.setState({ dragging: false });
  210. this.video.play();
  211. }
  212. handleMouseMove = throttle(e => {
  213. const { x } = getPointerPosition(this.seek, e);
  214. const currentTime = this.video.duration * x;
  215. if (!isNaN(currentTime)) {
  216. this.setState({ currentTime }, () => {
  217. this.video.currentTime = currentTime;
  218. });
  219. }
  220. }, 15);
  221. seekBy (time) {
  222. const currentTime = this.video.currentTime + time;
  223. if (!isNaN(currentTime)) {
  224. this.setState({ currentTime }, () => {
  225. this.video.currentTime = currentTime;
  226. });
  227. }
  228. }
  229. handleVideoKeyDown = e => {
  230. // On the video element or the seek bar, we can safely use the space bar
  231. // for playback control because there are no buttons to press
  232. if (e.key === ' ') {
  233. e.preventDefault();
  234. e.stopPropagation();
  235. this.togglePlay();
  236. }
  237. }
  238. handleKeyDown = e => {
  239. const frameTime = 1 / this.getFrameRate();
  240. switch(e.key) {
  241. case 'k':
  242. e.preventDefault();
  243. e.stopPropagation();
  244. this.togglePlay();
  245. break;
  246. case 'm':
  247. e.preventDefault();
  248. e.stopPropagation();
  249. this.toggleMute();
  250. break;
  251. case 'f':
  252. e.preventDefault();
  253. e.stopPropagation();
  254. this.toggleFullscreen();
  255. break;
  256. case 'j':
  257. e.preventDefault();
  258. e.stopPropagation();
  259. this.seekBy(-10);
  260. break;
  261. case 'l':
  262. e.preventDefault();
  263. e.stopPropagation();
  264. this.seekBy(10);
  265. break;
  266. case ',':
  267. e.preventDefault();
  268. e.stopPropagation();
  269. this.seekBy(-frameTime);
  270. break;
  271. case '.':
  272. e.preventDefault();
  273. e.stopPropagation();
  274. this.seekBy(frameTime);
  275. break;
  276. }
  277. // If we are in fullscreen mode, we don't want any hotkeys
  278. // interacting with the UI that's not visible
  279. if (this.state.fullscreen) {
  280. e.preventDefault();
  281. e.stopPropagation();
  282. if (e.key === 'Escape') {
  283. exitFullscreen();
  284. }
  285. }
  286. }
  287. togglePlay = () => {
  288. if (this.state.paused) {
  289. this.setState({ paused: false }, () => this.video.play());
  290. } else {
  291. this.setState({ paused: true }, () => this.video.pause());
  292. }
  293. }
  294. toggleFullscreen = () => {
  295. if (isFullscreen()) {
  296. exitFullscreen();
  297. } else {
  298. requestFullscreen(this.player);
  299. }
  300. }
  301. componentDidMount () {
  302. document.addEventListener('fullscreenchange', this.handleFullscreenChange, true);
  303. document.addEventListener('webkitfullscreenchange', this.handleFullscreenChange, true);
  304. document.addEventListener('mozfullscreenchange', this.handleFullscreenChange, true);
  305. document.addEventListener('MSFullscreenChange', this.handleFullscreenChange, true);
  306. window.addEventListener('scroll', this.handleScroll);
  307. window.addEventListener('resize', this.handleResize, { passive: true });
  308. }
  309. componentWillUnmount () {
  310. window.removeEventListener('scroll', this.handleScroll);
  311. window.removeEventListener('resize', this.handleResize);
  312. document.removeEventListener('fullscreenchange', this.handleFullscreenChange, true);
  313. document.removeEventListener('webkitfullscreenchange', this.handleFullscreenChange, true);
  314. document.removeEventListener('mozfullscreenchange', this.handleFullscreenChange, true);
  315. document.removeEventListener('MSFullscreenChange', this.handleFullscreenChange, true);
  316. if (!this.state.paused && this.video && this.props.deployPictureInPicture) {
  317. this.props.deployPictureInPicture('video', {
  318. src: this.props.src,
  319. currentTime: this.video.currentTime,
  320. muted: this.video.muted,
  321. volume: this.video.volume,
  322. });
  323. }
  324. }
  325. componentWillReceiveProps (nextProps) {
  326. if (!is(nextProps.visible, this.props.visible) && nextProps.visible !== undefined) {
  327. this.setState({ revealed: nextProps.visible });
  328. }
  329. }
  330. componentDidUpdate (prevProps, prevState) {
  331. if (prevState.revealed && !this.state.revealed && this.video) {
  332. this.video.pause();
  333. }
  334. }
  335. handleResize = debounce(() => {
  336. if (this.player) {
  337. this._setDimensions();
  338. }
  339. }, 250, {
  340. trailing: true,
  341. });
  342. handleScroll = throttle(() => {
  343. if (!this.video) {
  344. return;
  345. }
  346. const { top, height } = this.video.getBoundingClientRect();
  347. const inView = (top <= (window.innerHeight || document.documentElement.clientHeight)) && (top + height >= 0);
  348. if (!this.state.paused && !inView) {
  349. this.video.pause();
  350. if (this.props.deployPictureInPicture) {
  351. this.props.deployPictureInPicture('video', {
  352. src: this.props.src,
  353. currentTime: this.video.currentTime,
  354. muted: this.video.muted,
  355. volume: this.video.volume,
  356. });
  357. }
  358. this.setState({ paused: true });
  359. }
  360. }, 150, { trailing: true })
  361. handleFullscreenChange = () => {
  362. this.setState({ fullscreen: isFullscreen() });
  363. }
  364. handleMouseEnter = () => {
  365. this.setState({ hovered: true });
  366. }
  367. handleMouseLeave = () => {
  368. this.setState({ hovered: false });
  369. }
  370. toggleMute = () => {
  371. const muted = !this.video.muted;
  372. this.setState({ muted }, () => {
  373. this.video.muted = muted;
  374. });
  375. }
  376. toggleReveal = () => {
  377. if (this.props.onToggleVisibility) {
  378. this.props.onToggleVisibility();
  379. } else {
  380. this.setState({ revealed: !this.state.revealed });
  381. }
  382. }
  383. handleLoadedData = () => {
  384. const { currentTime, volume, muted, autoPlay } = this.props;
  385. if (currentTime) {
  386. this.video.currentTime = currentTime;
  387. }
  388. if (volume !== undefined) {
  389. this.video.volume = volume;
  390. }
  391. if (muted !== undefined) {
  392. this.video.muted = muted;
  393. }
  394. if (autoPlay) {
  395. this.video.play();
  396. }
  397. }
  398. handleProgress = () => {
  399. const lastTimeRange = this.video.buffered.length - 1;
  400. if (lastTimeRange > -1) {
  401. this.setState({ buffer: Math.ceil(this.video.buffered.end(lastTimeRange) / this.video.duration * 100) });
  402. }
  403. }
  404. handleVolumeChange = () => {
  405. this.setState({ volume: this.video.volume, muted: this.video.muted });
  406. }
  407. handleOpenVideo = () => {
  408. const { src, preview, width, height, alt } = this.props;
  409. const media = fromJS({
  410. type: 'video',
  411. url: src,
  412. preview_url: preview,
  413. description: alt,
  414. width,
  415. height,
  416. });
  417. const options = {
  418. startTime: this.video.currentTime,
  419. autoPlay: !this.state.paused,
  420. defaultVolume: this.state.volume,
  421. };
  422. this.video.pause();
  423. this.props.onOpenVideo(media, options);
  424. }
  425. handleCloseVideo = () => {
  426. this.video.pause();
  427. this.props.onCloseVideo();
  428. }
  429. getFrameRate () {
  430. if (this.props.frameRate && isNaN(this.props.frameRate)) {
  431. // The frame rate is returned as a fraction string so we
  432. // need to convert it to a number
  433. return this.props.frameRate.split('/').reduce((p, c) => p / c);
  434. }
  435. return this.props.frameRate;
  436. }
  437. render () {
  438. const { preview, src, inline, onOpenVideo, onCloseVideo, intl, alt, detailed, sensitive, editable, blurhash } = this.props;
  439. const { containerWidth, currentTime, duration, volume, buffer, dragging, paused, fullscreen, hovered, muted, revealed } = this.state;
  440. const progress = Math.min((currentTime / duration) * 100, 100);
  441. const playerStyle = {};
  442. let { width, height } = this.props;
  443. if (inline && containerWidth) {
  444. width = containerWidth;
  445. height = containerWidth / (16/9);
  446. playerStyle.height = height;
  447. }
  448. let preload;
  449. if (this.props.currentTime || fullscreen || dragging) {
  450. preload = 'auto';
  451. } else if (detailed) {
  452. preload = 'metadata';
  453. } else {
  454. preload = 'none';
  455. }
  456. let warning;
  457. if (sensitive) {
  458. warning = <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' />;
  459. } else {
  460. warning = <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />;
  461. }
  462. return (
  463. <div
  464. role='menuitem'
  465. className={classNames('video-player', { inactive: !revealed, detailed, inline: inline && !fullscreen, fullscreen, editable })}
  466. style={playerStyle}
  467. ref={this.setPlayerRef}
  468. onMouseEnter={this.handleMouseEnter}
  469. onMouseLeave={this.handleMouseLeave}
  470. onClick={this.handleClickRoot}
  471. onKeyDown={this.handleKeyDown}
  472. tabIndex={0}
  473. >
  474. <Blurhash
  475. hash={blurhash}
  476. className={classNames('media-gallery__preview', {
  477. 'media-gallery__preview--hidden': revealed,
  478. })}
  479. dummy={!useBlurhash}
  480. />
  481. {(revealed || editable) && <video
  482. ref={this.setVideoRef}
  483. src={src}
  484. poster={preview}
  485. preload={preload}
  486. role='button'
  487. tabIndex='0'
  488. aria-label={alt}
  489. title={alt}
  490. width={width}
  491. height={height}
  492. volume={volume}
  493. onClick={this.togglePlay}
  494. onKeyDown={this.handleVideoKeyDown}
  495. onPlay={this.handlePlay}
  496. onPause={this.handlePause}
  497. onLoadedData={this.handleLoadedData}
  498. onProgress={this.handleProgress}
  499. onVolumeChange={this.handleVolumeChange}
  500. />}
  501. <div className={classNames('spoiler-button', { 'spoiler-button--hidden': revealed || editable })}>
  502. <button type='button' className='spoiler-button__overlay' onClick={this.toggleReveal}>
  503. <span className='spoiler-button__overlay__label'>{warning}</span>
  504. </button>
  505. </div>
  506. <div className={classNames('video-player__controls', { active: paused || hovered })}>
  507. <div className='video-player__seek' onMouseDown={this.handleMouseDown} ref={this.setSeekRef}>
  508. <div className='video-player__seek__buffer' style={{ width: `${buffer}%` }} />
  509. <div className='video-player__seek__progress' style={{ width: `${progress}%` }} />
  510. <span
  511. className={classNames('video-player__seek__handle', { active: dragging })}
  512. tabIndex='0'
  513. style={{ left: `${progress}%` }}
  514. onKeyDown={this.handleVideoKeyDown}
  515. />
  516. </div>
  517. <div className='video-player__buttons-bar'>
  518. <div className='video-player__buttons left'>
  519. <button type='button' title={intl.formatMessage(paused ? messages.play : messages.pause)} aria-label={intl.formatMessage(paused ? messages.play : messages.pause)} className='player-button' onClick={this.togglePlay} autoFocus={detailed}><Icon id={paused ? 'play' : 'pause'} fixedWidth /></button>
  520. <button type='button' title={intl.formatMessage(muted ? messages.unmute : messages.mute)} aria-label={intl.formatMessage(muted ? messages.unmute : messages.mute)} className='player-button' onClick={this.toggleMute}><Icon id={muted ? 'volume-off' : 'volume-up'} fixedWidth /></button>
  521. <div className={classNames('video-player__volume', { active: this.state.hovered })} onMouseDown={this.handleVolumeMouseDown} ref={this.setVolumeRef}>
  522. <div className='video-player__volume__current' style={{ width: `${volume * 100}%` }} />
  523. <span
  524. className={classNames('video-player__volume__handle')}
  525. tabIndex='0'
  526. style={{ left: `${volume * 100}%` }}
  527. />
  528. </div>
  529. {(detailed || fullscreen) && (
  530. <span className='video-player__time'>
  531. <span className='video-player__time-current'>{formatTime(Math.floor(currentTime))}</span>
  532. <span className='video-player__time-sep'>/</span>
  533. <span className='video-player__time-total'>{formatTime(Math.floor(duration))}</span>
  534. </span>
  535. )}
  536. </div>
  537. <div className='video-player__buttons right'>
  538. {(!onCloseVideo && !editable && !fullscreen && !this.props.alwaysVisible) && <button type='button' title={intl.formatMessage(messages.hide)} aria-label={intl.formatMessage(messages.hide)} className='player-button' onClick={this.toggleReveal}><Icon id='eye-slash' fixedWidth /></button>}
  539. {(!fullscreen && onOpenVideo) && <button type='button' title={intl.formatMessage(messages.expand)} aria-label={intl.formatMessage(messages.expand)} className='player-button' onClick={this.handleOpenVideo}><Icon id='expand' fixedWidth /></button>}
  540. {onCloseVideo && <button type='button' title={intl.formatMessage(messages.close)} aria-label={intl.formatMessage(messages.close)} className='player-button' onClick={this.handleCloseVideo}><Icon id='compress' fixedWidth /></button>}
  541. <button type='button' title={intl.formatMessage(fullscreen ? messages.exit_fullscreen : messages.fullscreen)} aria-label={intl.formatMessage(fullscreen ? messages.exit_fullscreen : messages.fullscreen)} className='player-button' onClick={this.toggleFullscreen}><Icon id={fullscreen ? 'compress' : 'arrows-alt'} fixedWidth /></button>
  542. </div>
  543. </div>
  544. </div>
  545. </div>
  546. );
  547. }
  548. }