]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/standalone/videos/embed.ts
Store webtorrent chunks in indexdb
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / embed.ts
1 import './embed.scss'
2
3 import * as videojs from 'video.js'
4
5 import { VideoDetails } from '../../../../shared'
6 import { getVideojsOptions } from '../../assets/player/peertube-player'
7
8 function getVideoUrl (id: string) {
9 return window.location.origin + '/api/v1/videos/' + id
10 }
11
12 function loadVideoInfo (videoId: string): Promise<Response> {
13 return fetch(getVideoUrl(videoId))
14 }
15
16 function removeElement (element: HTMLElement) {
17 element.parentElement.removeChild(element)
18 }
19
20 function displayError (videoElement: HTMLVideoElement, text: string) {
21 // Remove video element
22 removeElement(videoElement)
23
24 document.title = 'Sorry - ' + text
25
26 const errorBlock = document.getElementById('error-block')
27 errorBlock.style.display = 'flex'
28
29 const errorText = document.getElementById('error-content')
30 errorText.innerHTML = text
31 }
32
33 function videoNotFound (videoElement: HTMLVideoElement) {
34 const text = 'This video does not exist.'
35 displayError(videoElement, text)
36 }
37
38 function videoFetchError (videoElement: HTMLVideoElement) {
39 const text = 'We cannot fetch the video. Please try again later.'
40 displayError(videoElement, text)
41 }
42
43 const urlParts = window.location.href.split('/')
44 const videoId = urlParts[urlParts.length - 1]
45
46 loadVideoInfo(videoId)
47 .then(async response => {
48 const videoContainerId = 'video-container'
49 const videoElement = document.getElementById(videoContainerId) as HTMLVideoElement
50
51 if (!response.ok) {
52 if (response.status === 404) return videoNotFound(videoElement)
53
54 return videoFetchError(videoElement)
55 }
56
57 const videoInfo: VideoDetails = await response.json()
58
59 let autoplay = false
60 let startTime = 0
61
62 try {
63 let params = new URL(window.location.toString()).searchParams
64 autoplay = params.has('autoplay') && (params.get('autoplay') === '1' || params.get('autoplay') === 'true')
65
66 const startTimeParamString = params.get('start')
67 const startTimeParamNumber = parseInt(startTimeParamString, 10)
68 if (isNaN(startTimeParamNumber) === false) startTime = startTimeParamNumber
69 } catch (err) {
70 console.error('Cannot get params from URL.', err)
71 }
72
73 const videojsOptions = getVideojsOptions({
74 autoplay,
75 inactivityTimeout: 1500,
76 videoViewUrl: getVideoUrl(videoId) + '/views',
77 playerElement: videoElement,
78 videoFiles: videoInfo.files,
79 videoDuration: videoInfo.duration,
80 enableHotkeys: true,
81 peertubeLink: true,
82 poster: window.location.origin + videoInfo.previewPath,
83 startTime
84 })
85 videojs(videoContainerId, videojsOptions, function () {
86 const player = this
87
88 player.dock({
89 title: videoInfo.name,
90 description: 'Uses P2P, others may know you are watching this video.'
91 })
92 })
93 })
94 .catch(err => console.error(err))