3 import videojs from 'video.js'
4 import 'videojs-dock/dist/videojs-dock.es.js'
5 import * as WebTorrent from 'webtorrent'
6 import { Video } from '../../../../shared'
8 // videojs typings don't have some method we need
9 const videojsUntyped = videojs as any
11 function loadVideoInfos (videoId: string, callback: (err: Error, res?: Video) => void) {
12 const xhttp = new XMLHttpRequest()
13 xhttp.onreadystatechange = function () {
14 if (this.readyState === 4 && this.status === 200) {
15 const json = JSON.parse(this.responseText)
16 return callback(null, json)
20 xhttp.onerror = err => callback(err.error)
22 const url = window.location.origin + '/api/v1/videos/' + videoId
23 xhttp.open('GET', url, true)
27 function loadVideoTorrent (magnetUri: string, player: videojs.Player) {
28 console.log('Loading video ' + videoId)
29 const client = new WebTorrent()
31 console.log('Adding magnet ' + magnetUri)
32 client.add(magnetUri, torrent => {
33 const file = torrent.files[0]
35 file.renderTo('video', err => {
41 // Hack to "simulate" src link in video.js >= 6
42 // If no, we can't play the video after pausing it
43 // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
44 (player as any).src = () => true
51 const urlParts = window.location.href.split('/')
52 const videoId = urlParts[urlParts.length - 1]
54 loadVideoInfos(videoId, (err, videoInfos) => {
60 const magnetUri = videoInfos.magnetUri
61 const videoContainer = document.getElementById('video-container') as HTMLVideoElement
62 const previewUrl = window.location.origin + videoInfos.previewPath
63 videoContainer.poster = previewUrl
65 videojs('video-container', { controls: true, autoplay: false }, function () {
68 const Button = videojsUntyped.getComponent('Button')
69 const peertubeLinkButton = videojsUntyped.extend(Button, {
70 constructor: function () {
71 Button.apply(this, arguments)
74 createEl: function () {
75 const link = document.createElement('a')
76 link.href = window.location.href.replace('embed', 'watch')
77 link.innerHTML = 'PeerTube'
78 link.title = 'Go to the video page'
79 link.className = 'vjs-peertube-link'
80 link.target = '_blank'
85 handleClick: function () {
89 videojsUntyped.registerComponent('PeerTubeLinkButton', peertubeLinkButton)
91 const controlBar = player.getChild('controlBar')
92 const addedLink = controlBar.addChild('PeerTubeLinkButton', {})
93 controlBar.el().insertBefore(addedLink.el(), controlBar.fullscreenToggle.el())
96 title: videoInfos.name
99 document.querySelector('.vjs-big-play-button').addEventListener('click', () => {
100 loadVideoTorrent(magnetUri, player)