From aa8b6df4a51c82eb91e6fd71a090b2128098af6b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 6 Oct 2017 10:40:09 +0200 Subject: Client: handle multiple file resolutions --- client/src/standalone/videos/embed.ts | 91 ++++++++--------------------------- 1 file changed, 20 insertions(+), 71 deletions(-) (limited to 'client/src/standalone/videos/embed.ts') diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index 0698344b0..f2f339bcc 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts @@ -1,14 +1,11 @@ import './embed.scss' import videojs from 'video.js' +import '../../assets/player/peertube-videojs-plugin' import 'videojs-dock/dist/videojs-dock.es.js' -import * as WebTorrent from 'webtorrent' import { Video } from '../../../../shared' -// videojs typings don't have some method we need -const videojsUntyped = videojs as any - -function loadVideoInfos (videoId: string, callback: (err: Error, res?: Video) => void) { +function loadVideoInfo (videoId: string, callback: (err: Error, res?: Video) => void) { const xhttp = new XMLHttpRequest() xhttp.onreadystatechange = function () { if (this.readyState === 4 && this.status === 200) { @@ -24,84 +21,36 @@ function loadVideoInfos (videoId: string, callback: (err: Error, res?: Video) => xhttp.send() } -function loadVideoTorrent (magnetUri: string, player: videojs.Player) { - console.log('Loading video ' + videoId) - const client = new WebTorrent() - - console.log('Adding magnet ' + magnetUri) - client.add(magnetUri, torrent => { - const file = torrent.files[0] - - file.renderTo('video', err => { - if (err) { - console.error(err) - return - } - - // Hack to "simulate" src link in video.js >= 6 - // If no, we can't play the video after pausing it - // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633 - (player as any).src = () => true - - player.play() - }) - }) -} - const urlParts = window.location.href.split('/') const videoId = urlParts[urlParts.length - 1] -loadVideoInfos(videoId, (err, videoInfos) => { +loadVideoInfo(videoId, (err, videoInfo) => { if (err) { console.error(err) return } - let magnetUri = '' - if (videoInfos.files !== undefined && videoInfos.files.length !== 0) { - magnetUri = videoInfos.files[0].magnetUri + const videoElement = document.getElementById('video-container') as HTMLVideoElement + const previewUrl = window.location.origin + videoInfo.previewPath + videoElement.poster = previewUrl + + const videojsOptions = { + controls: true, + autoplay: false, + plugins: { + peertube: { + videoFiles: videoInfo.files, + playerElement: videoElement, + autoplay: false, + peerTubeLink: true + } + } } - - const videoContainer = document.getElementById('video-container') as HTMLVideoElement - const previewUrl = window.location.origin + videoInfos.previewPath - videoContainer.poster = previewUrl - - videojs('video-container', { controls: true, autoplay: false }, function () { + videojs('video-container', videojsOptions, function () { const player = this - const Button = videojsUntyped.getComponent('Button') - const peertubeLinkButton = videojsUntyped.extend(Button, { - constructor: function () { - Button.apply(this, arguments) - }, - - createEl: function () { - const link = document.createElement('a') - link.href = window.location.href.replace('embed', 'watch') - link.innerHTML = 'PeerTube' - link.title = 'Go to the video page' - link.className = 'vjs-peertube-link' - link.target = '_blank' - - return link - }, - - handleClick: function () { - player.pause() - } - }) - videojsUntyped.registerComponent('PeerTubeLinkButton', peertubeLinkButton) - - const controlBar = player.getChild('controlBar') - const addedLink = controlBar.addChild('PeerTubeLinkButton', {}) - controlBar.el().insertBefore(addedLink.el(), controlBar.fullscreenToggle.el()) - player.dock({ - title: videoInfos.name + title: videoInfo.name }) - - document.querySelector('.vjs-big-play-button').addEventListener('click', () => { - loadVideoTorrent(magnetUri, player) - }, false) }) }) -- cgit v1.2.3