]>
Commit | Line | Data |
---|---|---|
aa8b6df4 C |
1 | // Big thanks to: https://github.com/kmoskwiak/videojs-resolution-switcher |
2 | ||
8cac1b64 | 3 | import { VideoService } from '@app/shared/video/video.service' |
63c4db6d | 4 | import * as videojs from 'video.js' |
aa8b6df4 | 5 | import * as WebTorrent from 'webtorrent' |
b6827820 | 6 | import { VideoFile } from '../../../../shared/models/videos/video.model' |
aa8b6df4 | 7 | import { renderVideo } from './video-renderer' |
be6a4802 | 8 | |
339632b4 C |
9 | declare module 'video.js' { |
10 | interface Player { | |
11 | peertube (): PeerTubePlugin | |
12 | } | |
13 | } | |
14 | ||
a22bfc3e | 15 | interface VideoJSComponentInterface { |
339632b4 | 16 | _player: videojs.Player |
a22bfc3e | 17 | |
339632b4 | 18 | new (player: videojs.Player, options?: any) |
a22bfc3e C |
19 | |
20 | registerComponent (name: string, obj: any) | |
21 | } | |
22 | ||
a22bfc3e C |
23 | type PeertubePluginOptions = { |
24 | videoFiles: VideoFile[] | |
25 | playerElement: HTMLVideoElement | |
26 | peerTubeLink: boolean | |
8cac1b64 | 27 | videoViewUrl: string |
a22bfc3e C |
28 | } |
29 | ||
be6a4802 C |
30 | // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts |
31 | // Don't import all Angular stuff, just copy the code with shame | |
32 | const dictionaryBytes: Array<{max: number, type: string}> = [ | |
33 | { max: 1024, type: 'B' }, | |
34 | { max: 1048576, type: 'KB' }, | |
35 | { max: 1073741824, type: 'MB' }, | |
36 | { max: 1.0995116e12, type: 'GB' } | |
37 | ] | |
38 | function bytes (value) { | |
39 | const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1] | |
40 | const calc = Math.floor(value / (format.max / 1024)).toString() | |
41 | ||
42 | return [ calc, format.type ] | |
43 | } | |
aa8b6df4 C |
44 | |
45 | // videojs typings don't have some method we need | |
46 | const videojsUntyped = videojs as any | |
47 | const webtorrent = new WebTorrent({ dht: false }) | |
48 | ||
a22bfc3e C |
49 | const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem') |
50 | class ResolutionMenuItem extends MenuItem { | |
51 | ||
339632b4 | 52 | constructor (player: videojs.Player, options) { |
aa8b6df4 | 53 | options.selectable = true |
a22bfc3e | 54 | super(player, options) |
aa8b6df4 | 55 | |
a22bfc3e | 56 | const currentResolution = this.player_.peertube().getCurrentResolution() |
aa8b6df4 | 57 | this.selected(this.options_.id === currentResolution) |
a22bfc3e | 58 | } |
aa8b6df4 | 59 | |
a22bfc3e | 60 | handleClick (event) { |
531ab5b6 C |
61 | super.handleClick(event) |
62 | ||
a22bfc3e | 63 | this.player_.peertube().updateResolution(this.options_.id) |
aa8b6df4 | 64 | } |
a22bfc3e | 65 | } |
aa8b6df4 C |
66 | MenuItem.registerComponent('ResolutionMenuItem', ResolutionMenuItem) |
67 | ||
a22bfc3e C |
68 | const MenuButton: VideoJSComponentInterface = videojsUntyped.getComponent('MenuButton') |
69 | class ResolutionMenuButton extends MenuButton { | |
70 | label: HTMLElement | |
71 | ||
339632b4 | 72 | constructor (player: videojs.Player, options) { |
aa8b6df4 | 73 | options.label = 'Quality' |
a22bfc3e C |
74 | super(player, options) |
75 | ||
76 | this.label = document.createElement('span') | |
aa8b6df4 | 77 | |
aa8b6df4 C |
78 | this.el().setAttribute('aria-label', 'Quality') |
79 | this.controlText('Quality') | |
80 | ||
81 | videojsUntyped.dom.addClass(this.label, 'vjs-resolution-button-label') | |
82 | this.el().appendChild(this.label) | |
83 | ||
a22bfc3e C |
84 | player.peertube().on('videoFileUpdate', () => this.update()) |
85 | } | |
aa8b6df4 | 86 | |
a22bfc3e | 87 | createItems () { |
aa8b6df4 | 88 | const menuItems = [] |
a22bfc3e | 89 | for (const videoFile of this.player_.peertube().videoFiles) { |
aa8b6df4 C |
90 | menuItems.push(new ResolutionMenuItem( |
91 | this.player_, | |
92 | { | |
93 | id: videoFile.resolution, | |
94 | label: videoFile.resolutionLabel, | |
95 | src: videoFile.magnetUri, | |
96 | selected: videoFile.resolution === this.currentSelection | |
97 | }) | |
98 | ) | |
99 | } | |
100 | ||
101 | return menuItems | |
a22bfc3e C |
102 | } |
103 | ||
104 | update () { | |
105 | if (!this.label) return | |
aa8b6df4 | 106 | |
a22bfc3e | 107 | this.label.innerHTML = this.player_.peertube().getCurrentResolutionLabel() |
be6a4802 | 108 | this.hide() |
a22bfc3e C |
109 | return super.update() |
110 | } | |
111 | ||
112 | buildCSSClass () { | |
113 | return super.buildCSSClass() + ' vjs-resolution-button' | |
114 | } | |
aa8b6df4 | 115 | |
a22bfc3e C |
116 | dispose () { |
117 | this.parentNode.removeChild(this) | |
aa8b6df4 | 118 | } |
a22bfc3e | 119 | } |
aa8b6df4 C |
120 | MenuButton.registerComponent('ResolutionMenuButton', ResolutionMenuButton) |
121 | ||
a22bfc3e C |
122 | const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button') |
123 | class PeertubeLinkButton extends Button { | |
aa8b6df4 | 124 | |
a22bfc3e | 125 | createEl () { |
aa8b6df4 C |
126 | const link = document.createElement('a') |
127 | link.href = window.location.href.replace('embed', 'watch') | |
128 | link.innerHTML = 'PeerTube' | |
129 | link.title = 'Go to the video page' | |
130 | link.className = 'vjs-peertube-link' | |
131 | link.target = '_blank' | |
132 | ||
133 | return link | |
a22bfc3e | 134 | } |
aa8b6df4 | 135 | |
a22bfc3e | 136 | handleClick () { |
be6a4802 | 137 | this.player_.pause() |
aa8b6df4 | 138 | } |
aa8b6df4 | 139 | |
a22bfc3e C |
140 | dispose () { |
141 | this.parentNode.removeChild(this) | |
142 | } | |
143 | } | |
144 | Button.registerComponent('PeerTubeLinkButton', PeertubeLinkButton) | |
be6a4802 | 145 | |
a22bfc3e C |
146 | class WebTorrentButton extends Button { |
147 | createEl () { | |
be6a4802 | 148 | const div = document.createElement('div') |
a06a31c7 C |
149 | const subDiv = document.createElement('div') |
150 | div.appendChild(subDiv) | |
be6a4802 C |
151 | |
152 | const downloadIcon = document.createElement('span') | |
153 | downloadIcon.classList.add('icon', 'icon-download') | |
a06a31c7 | 154 | subDiv.appendChild(downloadIcon) |
be6a4802 C |
155 | |
156 | const downloadSpeedText = document.createElement('span') | |
157 | downloadSpeedText.classList.add('download-speed-text') | |
158 | const downloadSpeedNumber = document.createElement('span') | |
159 | downloadSpeedNumber.classList.add('download-speed-number') | |
160 | const downloadSpeedUnit = document.createElement('span') | |
161 | downloadSpeedText.appendChild(downloadSpeedNumber) | |
162 | downloadSpeedText.appendChild(downloadSpeedUnit) | |
a06a31c7 | 163 | subDiv.appendChild(downloadSpeedText) |
be6a4802 C |
164 | |
165 | const uploadIcon = document.createElement('span') | |
166 | uploadIcon.classList.add('icon', 'icon-upload') | |
a06a31c7 | 167 | subDiv.appendChild(uploadIcon) |
be6a4802 C |
168 | |
169 | const uploadSpeedText = document.createElement('span') | |
170 | uploadSpeedText.classList.add('upload-speed-text') | |
171 | const uploadSpeedNumber = document.createElement('span') | |
172 | uploadSpeedNumber.classList.add('upload-speed-number') | |
173 | const uploadSpeedUnit = document.createElement('span') | |
174 | uploadSpeedText.appendChild(uploadSpeedNumber) | |
175 | uploadSpeedText.appendChild(uploadSpeedUnit) | |
a06a31c7 | 176 | subDiv.appendChild(uploadSpeedText) |
be6a4802 C |
177 | |
178 | const peersText = document.createElement('span') | |
179 | peersText.textContent = ' peers' | |
180 | peersText.classList.add('peers-text') | |
181 | const peersNumber = document.createElement('span') | |
182 | peersNumber.classList.add('peers-number') | |
a06a31c7 C |
183 | subDiv.appendChild(peersNumber) |
184 | subDiv.appendChild(peersText) | |
be6a4802 C |
185 | |
186 | div.className = 'vjs-webtorrent' | |
187 | // Hide the stats before we get the info | |
a86309b4 | 188 | subDiv.className = 'vjs-webtorrent-hidden' |
be6a4802 | 189 | |
a22bfc3e | 190 | this.player_.peertube().on('torrentInfo', (event, data) => { |
be6a4802 C |
191 | const downloadSpeed = bytes(data.downloadSpeed) |
192 | const uploadSpeed = bytes(data.uploadSpeed) | |
193 | const numPeers = data.numPeers | |
194 | ||
195 | downloadSpeedNumber.textContent = downloadSpeed[0] | |
196 | downloadSpeedUnit.textContent = ' ' + downloadSpeed[1] | |
197 | ||
198 | uploadSpeedNumber.textContent = uploadSpeed[0] | |
199 | uploadSpeedUnit.textContent = ' ' + uploadSpeed[1] | |
200 | ||
201 | peersNumber.textContent = numPeers | |
202 | ||
a86309b4 | 203 | subDiv.className = 'vjs-webtorrent-displayed' |
be6a4802 C |
204 | }) |
205 | ||
206 | return div | |
207 | } | |
be6a4802 | 208 | |
a22bfc3e C |
209 | dispose () { |
210 | this.parentNode.removeChild(this) | |
211 | } | |
aa8b6df4 | 212 | } |
a22bfc3e C |
213 | Button.registerComponent('WebTorrentButton', WebTorrentButton) |
214 | ||
215 | const Plugin: VideoJSComponentInterface = videojsUntyped.getPlugin('plugin') | |
216 | class PeerTubePlugin extends Plugin { | |
217 | private player: any | |
218 | private currentVideoFile: VideoFile | |
219 | private playerElement: HTMLVideoElement | |
220 | private videoFiles: VideoFile[] | |
221 | private torrent: WebTorrent.Torrent | |
481d3596 | 222 | private autoplay = false |
8cac1b64 C |
223 | private videoViewUrl: string |
224 | private videoViewInterval | |
a22bfc3e | 225 | |
339632b4 | 226 | constructor (player: videojs.Player, options: PeertubePluginOptions) { |
a22bfc3e C |
227 | super(player, options) |
228 | ||
481d3596 C |
229 | // Fix canplay event on google chrome by disabling default videojs autoplay |
230 | this.autoplay = this.player.options_.autoplay | |
231 | this.player.options_.autoplay = false | |
232 | ||
a22bfc3e | 233 | this.videoFiles = options.videoFiles |
8cac1b64 | 234 | this.videoViewUrl = options.videoViewUrl |
a22bfc3e C |
235 | |
236 | // Hack to "simulate" src link in video.js >= 6 | |
237 | // Without this, we can't play the video after pausing it | |
238 | // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633 | |
239 | this.player.src = function () { | |
240 | return true | |
241 | } | |
242 | ||
243 | this.playerElement = options.playerElement | |
244 | ||
245 | this.player.ready(() => { | |
246 | this.initializePlayer(options) | |
247 | this.runTorrentInfoScheduler() | |
8cac1b64 | 248 | this.prepareRunViewAdd() |
a22bfc3e C |
249 | }) |
250 | } | |
251 | ||
252 | dispose () { | |
253 | // Don't need to destroy renderer, video player will be destroyed | |
254 | this.flushVideoFile(this.currentVideoFile, false) | |
aa8b6df4 C |
255 | } |
256 | ||
a22bfc3e C |
257 | getCurrentResolution () { |
258 | return this.currentVideoFile ? this.currentVideoFile.resolution : -1 | |
aa8b6df4 C |
259 | } |
260 | ||
a22bfc3e C |
261 | getCurrentResolutionLabel () { |
262 | return this.currentVideoFile ? this.currentVideoFile.resolutionLabel : '' | |
aa8b6df4 C |
263 | } |
264 | ||
a22bfc3e | 265 | updateVideoFile (videoFile?: VideoFile, done?: () => void) { |
aa8b6df4 C |
266 | if (done === undefined) { |
267 | done = () => { /* empty */ } | |
268 | } | |
269 | ||
270 | // Pick the first one | |
271 | if (videoFile === undefined) { | |
a22bfc3e | 272 | videoFile = this.videoFiles[0] |
aa8b6df4 C |
273 | } |
274 | ||
275 | // Don't add the same video file once again | |
a22bfc3e | 276 | if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) { |
aa8b6df4 C |
277 | return |
278 | } | |
279 | ||
a22bfc3e C |
280 | const previousVideoFile = this.currentVideoFile |
281 | this.currentVideoFile = videoFile | |
aa8b6df4 C |
282 | |
283 | console.log('Adding ' + videoFile.magnetUri + '.') | |
a22bfc3e | 284 | this.torrent = webtorrent.add(videoFile.magnetUri, torrent => { |
aa8b6df4 C |
285 | console.log('Added ' + videoFile.magnetUri + '.') |
286 | ||
287 | this.flushVideoFile(previousVideoFile) | |
288 | ||
289 | const options = { autoplay: true, controls: true } | |
a22bfc3e C |
290 | renderVideo(torrent.files[0], this.playerElement, options,(err, renderer) => { |
291 | if (err) return this.handleError(err) | |
aa8b6df4 C |
292 | |
293 | this.renderer = renderer | |
531ab5b6 | 294 | if (!this.player.paused()) this.player.play().then(done) |
481d3596 | 295 | else done() |
aa8b6df4 C |
296 | }) |
297 | }) | |
298 | ||
a22bfc3e C |
299 | this.torrent.on('error', err => this.handleError(err)) |
300 | this.torrent.on('warning', (err: any) => { | |
a96aed15 | 301 | // We don't support HTTP tracker but we don't care -> we use the web socket tracker |
531ab5b6 | 302 | if (err.message.indexOf('Unsupported tracker protocol') !== -1) return |
7dbdc3ba C |
303 | // Users don't care about issues with WebRTC, but developers do so log it in the console |
304 | if (err.message.indexOf('Ice connection failed') !== -1) { | |
305 | console.error(err) | |
306 | return | |
307 | } | |
a96aed15 | 308 | |
a22bfc3e | 309 | return this.handleError(err) |
a96aed15 | 310 | }) |
aa8b6df4 | 311 | |
a22bfc3e | 312 | this.trigger('videoFileUpdate') |
aa8b6df4 C |
313 | } |
314 | ||
a22bfc3e | 315 | updateResolution (resolution) { |
aa8b6df4 | 316 | // Remember player state |
a22bfc3e C |
317 | const currentTime = this.player.currentTime() |
318 | const isPaused = this.player.paused() | |
aa8b6df4 | 319 | |
531ab5b6 C |
320 | // Remove poster to have black background |
321 | this.playerElement.poster = '' | |
322 | ||
aa8b6df4 | 323 | // Hide bigPlayButton |
8fa5653a | 324 | if (!isPaused) { |
a22bfc3e | 325 | this.player.bigPlayButton.hide() |
aa8b6df4 C |
326 | } |
327 | ||
a22bfc3e C |
328 | const newVideoFile = this.videoFiles.find(f => f.resolution === resolution) |
329 | this.updateVideoFile(newVideoFile, () => { | |
330 | this.player.currentTime(currentTime) | |
331 | this.player.handleTechSeeked_() | |
aa8b6df4 C |
332 | }) |
333 | } | |
334 | ||
a22bfc3e | 335 | flushVideoFile (videoFile: VideoFile, destroyRenderer = true) { |
aa8b6df4 C |
336 | if (videoFile !== undefined && webtorrent.get(videoFile.magnetUri)) { |
337 | if (destroyRenderer === true) this.renderer.destroy() | |
338 | webtorrent.remove(videoFile.magnetUri) | |
a22bfc3e | 339 | console.log('Removed ' + videoFile.magnetUri) |
aa8b6df4 C |
340 | } |
341 | } | |
342 | ||
8cac1b64 C |
343 | setVideoFiles (files: VideoFile[], videoViewUrl: string) { |
344 | this.videoViewUrl = videoViewUrl | |
a22bfc3e | 345 | this.videoFiles = files |
ed9f9f5f | 346 | |
8cac1b64 C |
347 | // Re run view add for the new video |
348 | this.prepareRunViewAdd() | |
a22bfc3e | 349 | this.updateVideoFile(undefined, () => this.player.play()) |
ed9f9f5f C |
350 | } |
351 | ||
a22bfc3e C |
352 | private initializePlayer (options: PeertubePluginOptions) { |
353 | const controlBar = this.player.controlBar | |
aa8b6df4 | 354 | |
a22bfc3e | 355 | const menuButton = new ResolutionMenuButton(this.player, options) |
aa8b6df4 C |
356 | const fullscreenElement = controlBar.fullscreenToggle.el() |
357 | controlBar.resolutionSwitcher = controlBar.el().insertBefore(menuButton.el(), fullscreenElement) | |
aa8b6df4 C |
358 | |
359 | if (options.peerTubeLink === true) { | |
a22bfc3e | 360 | const peerTubeLinkButton = new PeertubeLinkButton(this.player) |
aa8b6df4 | 361 | controlBar.peerTubeLink = controlBar.el().insertBefore(peerTubeLinkButton.el(), fullscreenElement) |
aa8b6df4 C |
362 | } |
363 | ||
a22bfc3e | 364 | const webTorrentButton = new WebTorrentButton(this.player) |
be6a4802 | 365 | controlBar.webTorrent = controlBar.el().insertBefore(webTorrentButton.el(), controlBar.progressControl.el()) |
be6a4802 | 366 | |
481d3596 C |
367 | if (this.autoplay === true) { |
368 | this.updateVideoFile(undefined, () => this.player.play()) | |
aa8b6df4 | 369 | } else { |
a22bfc3e | 370 | this.player.one('play', () => { |
481d3596 C |
371 | this.player.pause() |
372 | this.updateVideoFile(undefined, () => this.player.play()) | |
4dd551a0 | 373 | }) |
aa8b6df4 | 374 | } |
a22bfc3e | 375 | } |
aa8b6df4 | 376 | |
a22bfc3e | 377 | private runTorrentInfoScheduler () { |
aa8b6df4 | 378 | setInterval(() => { |
a22bfc3e C |
379 | if (this.torrent !== undefined) { |
380 | this.trigger('torrentInfo', { | |
381 | downloadSpeed: this.torrent.downloadSpeed, | |
382 | numPeers: this.torrent.numPeers, | |
383 | uploadSpeed: this.torrent.uploadSpeed | |
aa8b6df4 C |
384 | }) |
385 | } | |
386 | }, 1000) | |
a22bfc3e | 387 | } |
aa8b6df4 | 388 | |
8cac1b64 C |
389 | private prepareRunViewAdd () { |
390 | if (this.player.readyState() < 1) { | |
391 | return this.player.one('loadedmetadata', () => this.runViewAdd()) | |
392 | } | |
393 | ||
394 | this.runViewAdd() | |
395 | } | |
396 | ||
397 | private runViewAdd () { | |
398 | this.clearVideoViewInterval() | |
399 | ||
400 | // After 30 seconds (or 3/4 of the video), add a view to the video | |
401 | let minSecondsToView = 30 | |
402 | ||
403 | const duration = this.player.duration() | |
404 | if (duration < minSecondsToView) minSecondsToView = (duration * 3) / 4 | |
405 | ||
406 | let secondsViewed = 0 | |
407 | this.videoViewInterval = setInterval(() => { | |
408 | if (this.player && !this.player.paused()) { | |
409 | secondsViewed += 1 | |
410 | ||
411 | if (secondsViewed > minSecondsToView) { | |
412 | this.clearVideoViewInterval() | |
413 | ||
414 | this.addViewToVideo().catch(err => console.error(err)) | |
415 | } | |
416 | } | |
417 | }, 1000) | |
418 | } | |
419 | ||
420 | private clearVideoViewInterval () { | |
421 | if (this.videoViewInterval !== undefined) { | |
422 | clearInterval(this.videoViewInterval) | |
423 | this.videoViewInterval = undefined | |
424 | } | |
425 | } | |
426 | ||
427 | private addViewToVideo () { | |
428 | return fetch(this.videoViewUrl, { method: 'POST' }) | |
429 | } | |
430 | ||
a22bfc3e C |
431 | private handleError (err: Error | string) { |
432 | return this.player.trigger('customError', { err }) | |
aa8b6df4 C |
433 | } |
434 | } | |
a22bfc3e | 435 | videojsUntyped.registerPlugin('peertube', PeerTubePlugin) |