]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/webtorrent/webtorrent-plugin.ts
refactor(wt-plugin): tighten TS definitions
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / webtorrent / webtorrent-plugin.ts
1 import videojs from 'video.js'
2 import * as WebTorrent from 'webtorrent'
3 import { renderVideo } from './video-renderer'
4 import { LoadedQualityData, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings'
5 import { getRtcConfig, timeToInt, videoFileMaxByResolution, videoFileMinByResolution, isIOS, isSafari } from '../utils'
6 import { PeertubeChunkStore } from './peertube-chunk-store'
7 import {
8 getAverageBandwidthInStore,
9 getStoredMute,
10 getStoredP2PEnabled,
11 getStoredVolume,
12 saveAverageBandwidth
13 } from '../peertube-player-local-storage'
14 import { VideoFile } from '@shared/models'
15
16 const CacheChunkStore = require('cache-chunk-store')
17
18 type PlayOptions = {
19 forcePlay?: boolean,
20 seek?: number,
21 delay?: number
22 }
23
24 const Plugin = videojs.getPlugin('plugin')
25
26 class WebTorrentPlugin extends Plugin {
27 readonly videoFiles: VideoFile[]
28
29 private readonly playerElement: HTMLVideoElement
30
31 private readonly autoplay: boolean = false
32 private readonly startTime: number = 0
33 private readonly savePlayerSrcFunction: videojs.Player['src']
34 private readonly videoDuration: number
35 private readonly CONSTANTS = {
36 INFO_SCHEDULER: 1000, // Don't change this
37 AUTO_QUALITY_SCHEDULER: 3000, // Check quality every 3 seconds
38 AUTO_QUALITY_THRESHOLD_PERCENT: 30, // Bandwidth should be 30% more important than a resolution bitrate to change to it
39 AUTO_QUALITY_OBSERVATION_TIME: 10000, // Wait 10 seconds after having change the resolution before another check
40 AUTO_QUALITY_HIGHER_RESOLUTION_DELAY: 5000, // Buffering higher resolution during 5 seconds
41 BANDWIDTH_AVERAGE_NUMBER_OF_VALUES: 5 // Last 5 seconds to build average bandwidth
42 }
43
44 private readonly webtorrent = new WebTorrent({
45 tracker: {
46 rtcConfig: getRtcConfig()
47 },
48 dht: false
49 })
50
51 private currentVideoFile: VideoFile
52 private torrent: WebTorrent.Torrent
53
54 private renderer: any
55 private fakeRenderer: any
56 private destroyingFakeRenderer = false
57
58 private autoResolution = true
59 private autoResolutionPossible = true
60 private isAutoResolutionObservation = false
61 private playerRefusedP2P = false
62
63 private torrentInfoInterval: any
64 private autoQualityInterval: any
65 private addTorrentDelay: any
66 private qualityObservationTimer: any
67 private runAutoQualitySchedulerTimer: any
68
69 private downloadSpeeds: number[] = []
70
71 constructor (player: videojs.Player, options: WebtorrentPluginOptions) {
72 super(player)
73
74 this.startTime = timeToInt(options.startTime)
75
76 // Disable auto play on iOS
77 this.autoplay = options.autoplay
78 this.playerRefusedP2P = !getStoredP2PEnabled()
79
80 this.videoFiles = options.videoFiles
81 this.videoDuration = options.videoDuration
82
83 this.savePlayerSrcFunction = this.player.src
84 this.playerElement = options.playerElement
85
86 this.player.ready(() => {
87 const playerOptions = this.player.options_
88
89 const volume = getStoredVolume()
90 if (volume !== undefined) this.player.volume(volume)
91
92 const muted = playerOptions.muted !== undefined ? playerOptions.muted : getStoredMute()
93 if (muted !== undefined) this.player.muted(muted)
94
95 this.player.duration(options.videoDuration)
96
97 this.initializePlayer()
98 this.runTorrentInfoScheduler()
99
100 this.player.one('play', () => {
101 // Don't run immediately scheduler, wait some seconds the TCP connections are made
102 this.runAutoQualitySchedulerTimer = setTimeout(() => this.runAutoQualityScheduler(), this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
103 })
104 })
105 }
106
107 dispose () {
108 clearTimeout(this.addTorrentDelay)
109 clearTimeout(this.qualityObservationTimer)
110 clearTimeout(this.runAutoQualitySchedulerTimer)
111
112 clearInterval(this.torrentInfoInterval)
113 clearInterval(this.autoQualityInterval)
114
115 // Don't need to destroy renderer, video player will be destroyed
116 this.flushVideoFile(this.currentVideoFile, false)
117
118 this.destroyFakeRenderer()
119 }
120
121 getCurrentResolutionId () {
122 return this.currentVideoFile ? this.currentVideoFile.resolution.id : -1
123 }
124
125 private getVideoFile(): VideoFile {
126 const savedAverageBandwidth = getAverageBandwidthInStore()
127 return savedAverageBandwidth
128 ? this.getAppropriateFile(savedAverageBandwidth)
129 : this.pickAverageVideoFile()
130 }
131
132 private updateVideoFile (
133 videoFile: VideoFile,
134 options: {
135 forcePlay?: boolean,
136 seek?: number,
137 delay?: number
138 } = {},
139 done: () => void = () => { /* empty */ }
140 ) {
141 if (videoFile === undefined) {
142 throw Error(`Can't update video file since videoFile is undefined.`)
143 }
144
145 // Don't add the same video file once again
146 if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) {
147 return
148 }
149
150 // Do not display error to user because we will have multiple fallback
151 this.disableErrorDisplay();
152
153 // Hack to "simulate" src link in video.js >= 6
154 // Without this, we can't play the video after pausing it
155 // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
156 (this.player as any).src = () => true
157 const oldPlaybackRate = this.player.playbackRate()
158
159 const previousVideoFile = this.currentVideoFile
160 this.currentVideoFile = videoFile
161
162 // Don't try on iOS that does not support MediaSource
163 // Or don't use P2P if webtorrent is disabled
164 if (isIOS() || this.playerRefusedP2P) {
165 return this.fallbackToHttp(options, () => {
166 this.player.playbackRate(oldPlaybackRate)
167 return done()
168 })
169 }
170
171 this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, options, () => {
172 this.player.playbackRate(oldPlaybackRate)
173 return done()
174 })
175
176 this.changeQuality()
177 this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.currentVideoFile.resolution.id })
178 }
179
180 updateResolution (resolutionId: number, delay = 0) {
181 // Remember player state
182 const currentTime = this.player.currentTime()
183 const isPaused = this.player.paused()
184
185 // Hide bigPlayButton
186 if (!isPaused) {
187 this.player.bigPlayButton.hide()
188 }
189
190 // Audio-only (resolutionId === 0) gets special treatment
191 if (resolutionId === 0) {
192 // Audio-only: show poster, do not auto-hide controls
193 this.player.addClass('vjs-playing-audio-only-content')
194 this.player.posterImage.show()
195 } else {
196 // Hide poster to have black background
197 this.player.removeClass('vjs-playing-audio-only-content')
198 this.player.posterImage.hide()
199 }
200
201 const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId)
202 const options = {
203 forcePlay: false,
204 delay,
205 seek: currentTime + (delay / 1000)
206 }
207
208 this.updateVideoFile(newVideoFile, options)
209 }
210
211 flushVideoFile (videoFile: VideoFile, destroyRenderer = true) {
212 if (videoFile !== undefined && this.webtorrent.get(videoFile.magnetUri)) {
213 if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy()
214
215 this.webtorrent.remove(videoFile.magnetUri)
216 console.log('Removed ' + videoFile.magnetUri)
217 }
218 }
219
220 enableAutoResolution () {
221 this.autoResolution = true
222 this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() })
223 }
224
225 disableAutoResolution (forbid = false) {
226 if (forbid === true) this.autoResolutionPossible = false
227
228 this.autoResolution = false
229 this.trigger('autoResolutionChange', { possible: this.autoResolutionPossible })
230 this.trigger('resolutionChange', { auto: this.autoResolution, resolutionId: this.getCurrentResolutionId() })
231 }
232
233 isAutoResolutionPossible () {
234 return this.autoResolutionPossible
235 }
236
237 getTorrent () {
238 return this.torrent
239 }
240
241 getCurrentVideoFile () {
242 return this.currentVideoFile
243 }
244
245 private addTorrent (
246 magnetOrTorrentUrl: string,
247 previousVideoFile: VideoFile,
248 options: PlayOptions,
249 done: Function
250 ) {
251 console.log('Adding ' + magnetOrTorrentUrl + '.')
252
253 const oldTorrent = this.torrent
254 const torrentOptions = {
255 // Don't use arrow function: it breaks webtorrent (that uses `new` keyword)
256 store: function (chunkLength: number, storeOpts: any) {
257 return new CacheChunkStore(new PeertubeChunkStore(chunkLength, storeOpts), {
258 max: 100
259 })
260 }
261 }
262
263 this.torrent = this.webtorrent.add(magnetOrTorrentUrl, torrentOptions, torrent => {
264 console.log('Added ' + magnetOrTorrentUrl + '.')
265
266 if (oldTorrent) {
267 // Pause the old torrent
268 this.stopTorrent(oldTorrent)
269
270 // We use a fake renderer so we download correct pieces of the next file
271 if (options.delay) this.renderFileInFakeElement(torrent.files[ 0 ], options.delay)
272 }
273
274 // Render the video in a few seconds? (on resolution change for example, we wait some seconds of the new video resolution)
275 this.addTorrentDelay = setTimeout(() => {
276 // We don't need the fake renderer anymore
277 this.destroyFakeRenderer()
278
279 const paused = this.player.paused()
280
281 this.flushVideoFile(previousVideoFile)
282
283 // Update progress bar (just for the UI), do not wait rendering
284 if (options.seek) this.player.currentTime(options.seek)
285
286 const renderVideoOptions = { autoplay: false, controls: true }
287 renderVideo(torrent.files[ 0 ], this.playerElement, renderVideoOptions, (err, renderer) => {
288 this.renderer = renderer
289
290 if (err) return this.fallbackToHttp(options, done)
291
292 return this.tryToPlay(err => {
293 if (err) return done(err)
294
295 if (options.seek) this.seek(options.seek)
296 if (options.forcePlay === false && paused === true) this.player.pause()
297
298 return done()
299 })
300 })
301 }, options.delay || 0)
302 })
303
304 this.torrent.on('error', (err: any) => console.error(err))
305
306 this.torrent.on('warning', (err: any) => {
307 // We don't support HTTP tracker but we don't care -> we use the web socket tracker
308 if (err.message.indexOf('Unsupported tracker protocol') !== -1) return
309
310 // Users don't care about issues with WebRTC, but developers do so log it in the console
311 if (err.message.indexOf('Ice connection failed') !== -1) {
312 console.log(err)
313 return
314 }
315
316 // Magnet hash is not up to date with the torrent file, add directly the torrent file
317 if (err.message.indexOf('incorrect info hash') !== -1) {
318 console.error('Incorrect info hash detected, falling back to torrent file.')
319 const newOptions = { forcePlay: true, seek: options.seek }
320 return this.addTorrent(this.torrent[ 'xs' ], previousVideoFile, newOptions, done)
321 }
322
323 // Remote instance is down
324 if (err.message.indexOf('from xs param') !== -1) {
325 this.handleError(err)
326 }
327
328 console.warn(err)
329 })
330 }
331
332 private tryToPlay (done?: (err?: Error) => void) {
333 if (!done) done = function () { /* empty */ }
334
335 const playPromise = this.player.play()
336 if (playPromise !== undefined) {
337 return playPromise.then(() => done())
338 .catch((err: Error) => {
339 if (err.message.indexOf('The play() request was interrupted by a call to pause()') !== -1) {
340 return
341 }
342
343 console.error(err)
344 this.player.pause()
345 this.player.posterImage.show()
346 this.player.removeClass('vjs-has-autoplay')
347 this.player.removeClass('vjs-has-big-play-button-clicked')
348 this.player.removeClass('vjs-playing-audio-only-content')
349
350 return done()
351 })
352 }
353
354 return done()
355 }
356
357 private seek (time: number) {
358 this.player.currentTime(time)
359 this.player.handleTechSeeked_()
360 }
361
362 private getAppropriateFile (averageDownloadSpeed?: number): VideoFile {
363 if (this.videoFiles === undefined) return undefined
364
365 const files = this.videoFiles.filter(f => f.resolution.id !== 0)
366
367 if (files.length === 0) return undefined
368 if (files.length === 1) return files[0]
369
370 // Don't change the torrent if the player ended
371 if (this.torrent && this.torrent.progress === 1 && this.player.ended()) return this.currentVideoFile
372
373 if (!averageDownloadSpeed) averageDownloadSpeed = this.getAndSaveActualDownloadSpeed()
374
375 // Limit resolution according to player height
376 const playerHeight = this.playerElement.offsetHeight
377
378 // We take the first resolution just above the player height
379 // Example: player height is 530px, we want the 720p file instead of 480p
380 let maxResolution = files[0].resolution.id
381 for (let i = files.length - 1; i >= 0; i--) {
382 const resolutionId = files[i].resolution.id
383 if (resolutionId !== 0 && resolutionId >= playerHeight) {
384 maxResolution = resolutionId
385 break
386 }
387 }
388
389 // Filter videos we can play according to our screen resolution and bandwidth
390 const filteredFiles = files.filter(f => f.resolution.id <= maxResolution)
391 .filter(f => {
392 const fileBitrate = (f.size / this.videoDuration)
393 let threshold = fileBitrate
394
395 // If this is for a higher resolution or an initial load: add a margin
396 if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) {
397 threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100)
398 }
399
400 return averageDownloadSpeed > threshold
401 })
402
403 // If the download speed is too bad, return the lowest resolution we have
404 if (filteredFiles.length === 0) return videoFileMinByResolution(files)
405
406 return videoFileMaxByResolution(filteredFiles)
407 }
408
409 private getAndSaveActualDownloadSpeed () {
410 const start = Math.max(this.downloadSpeeds.length - this.CONSTANTS.BANDWIDTH_AVERAGE_NUMBER_OF_VALUES, 0)
411 const lastDownloadSpeeds = this.downloadSpeeds.slice(start, this.downloadSpeeds.length)
412 if (lastDownloadSpeeds.length === 0) return -1
413
414 const sum = lastDownloadSpeeds.reduce((a, b) => a + b)
415 const averageBandwidth = Math.round(sum / lastDownloadSpeeds.length)
416
417 // Save the average bandwidth for future use
418 saveAverageBandwidth(averageBandwidth)
419
420 return averageBandwidth
421 }
422
423 private initializePlayer () {
424 this.buildQualities()
425
426 if (this.autoplay) {
427 this.player.posterImage.hide()
428
429 return this.updateVideoFile(this.getVideoFile(), { forcePlay: true, seek: this.startTime })
430 }
431
432 // Proxy first play
433 const oldPlay = this.player.play.bind(this.player);
434 (this.player as any).play = () => {
435 this.player.addClass('vjs-has-big-play-button-clicked')
436 this.player.play = oldPlay
437
438 this.updateVideoFile(this.getVideoFile(), { forcePlay: true, seek: this.startTime })
439 }
440 }
441
442 private runAutoQualityScheduler () {
443 this.autoQualityInterval = setInterval(() => {
444
445 // Not initialized or in HTTP fallback
446 if (this.torrent === undefined || this.torrent === null) return
447 if (this.autoResolution === false) return
448 if (this.isAutoResolutionObservation === true) return
449
450 const file = this.getAppropriateFile()
451 let changeResolution = false
452 let changeResolutionDelay = 0
453
454 // Lower resolution
455 if (this.isPlayerWaiting() && file.resolution.id < this.currentVideoFile.resolution.id) {
456 console.log('Downgrading automatically the resolution to: %s', file.resolution.label)
457 changeResolution = true
458 } else if (file.resolution.id > this.currentVideoFile.resolution.id) { // Higher resolution
459 console.log('Upgrading automatically the resolution to: %s', file.resolution.label)
460 changeResolution = true
461 changeResolutionDelay = this.CONSTANTS.AUTO_QUALITY_HIGHER_RESOLUTION_DELAY
462 }
463
464 if (changeResolution === true) {
465 this.updateResolution(file.resolution.id, changeResolutionDelay)
466
467 // Wait some seconds in observation of our new resolution
468 this.isAutoResolutionObservation = true
469
470 this.qualityObservationTimer = setTimeout(() => {
471 this.isAutoResolutionObservation = false
472 }, this.CONSTANTS.AUTO_QUALITY_OBSERVATION_TIME)
473 }
474 }, this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
475 }
476
477 private isPlayerWaiting () {
478 return this.player && this.player.hasClass('vjs-waiting')
479 }
480
481 private runTorrentInfoScheduler () {
482 this.torrentInfoInterval = setInterval(() => {
483 // Not initialized yet
484 if (this.torrent === undefined) return
485
486 // Http fallback
487 if (this.torrent === null) return this.player.trigger('p2pInfo', false)
488
489 // this.webtorrent.downloadSpeed because we need to take into account the potential old torrent too
490 if (this.webtorrent.downloadSpeed !== 0) this.downloadSpeeds.push(this.webtorrent.downloadSpeed)
491
492 return this.player.trigger('p2pInfo', {
493 source: 'webtorrent',
494 http: {
495 downloadSpeed: 0,
496 uploadSpeed: 0,
497 downloaded: 0,
498 uploaded: 0
499 },
500 p2p: {
501 downloadSpeed: this.torrent.downloadSpeed,
502 numPeers: this.torrent.numPeers,
503 uploadSpeed: this.torrent.uploadSpeed,
504 downloaded: this.torrent.downloaded,
505 uploaded: this.torrent.uploaded
506 }
507 } as PlayerNetworkInfo)
508 }, this.CONSTANTS.INFO_SCHEDULER)
509 }
510
511 private fallbackToHttp (options: PlayOptions, done?: Function) {
512 const paused = this.player.paused()
513
514 this.disableAutoResolution(true)
515
516 this.flushVideoFile(this.currentVideoFile, true)
517 this.torrent = null
518
519 // Enable error display now this is our last fallback
520 this.player.one('error', () => this.enableErrorDisplay())
521
522 const httpUrl = this.currentVideoFile.fileUrl
523 this.player.src = this.savePlayerSrcFunction
524 this.player.src(httpUrl)
525
526 this.changeQuality()
527
528 // We changed the source, so reinit captions
529 this.player.trigger('sourcechange')
530
531 return this.tryToPlay(err => {
532 if (err && done) return done(err)
533
534 if (options.seek) this.seek(options.seek)
535 if (options.forcePlay === false && paused === true) this.player.pause()
536
537 if (done) return done()
538 })
539 }
540
541 private handleError (err: Error | string) {
542 return this.player.trigger('customError', { err })
543 }
544
545 private enableErrorDisplay () {
546 this.player.addClass('vjs-error-display-enabled')
547 }
548
549 private disableErrorDisplay () {
550 this.player.removeClass('vjs-error-display-enabled')
551 }
552
553 private pickAverageVideoFile () {
554 if (this.videoFiles.length === 1) return this.videoFiles[0]
555
556 return this.videoFiles[Math.floor(this.videoFiles.length / 2)]
557 }
558
559 private stopTorrent (torrent: WebTorrent.Torrent) {
560 torrent.pause()
561 // Pause does not remove actual peers (in particular the webseed peer)
562 torrent.removePeer(torrent[ 'ws' ])
563 }
564
565 private renderFileInFakeElement (file: WebTorrent.TorrentFile, delay: number) {
566 this.destroyingFakeRenderer = false
567
568 const fakeVideoElem = document.createElement('video')
569 renderVideo(file, fakeVideoElem, { autoplay: false, controls: false }, (err, renderer) => {
570 this.fakeRenderer = renderer
571
572 // The renderer returns an error when we destroy it, so skip them
573 if (this.destroyingFakeRenderer === false && err) {
574 console.error('Cannot render new torrent in fake video element.', err)
575 }
576
577 // Load the future file at the correct time (in delay MS - 2 seconds)
578 fakeVideoElem.currentTime = this.player.currentTime() + (delay - 2000)
579 })
580 }
581
582 private destroyFakeRenderer () {
583 if (this.fakeRenderer) {
584 this.destroyingFakeRenderer = true
585
586 if (this.fakeRenderer.destroy) {
587 try {
588 this.fakeRenderer.destroy()
589 } catch (err) {
590 console.log('Cannot destroy correctly fake renderer.', err)
591 }
592 }
593 this.fakeRenderer = undefined
594 }
595 }
596
597 private buildQualities () {
598 const qualityLevelsPayload = []
599
600 for (const file of this.videoFiles) {
601 const representation = {
602 id: file.resolution.id,
603 label: this.buildQualityLabel(file),
604 height: file.resolution.id,
605 _enabled: true
606 }
607
608 this.player.qualityLevels().addQualityLevel(representation)
609
610 qualityLevelsPayload.push({
611 id: representation.id,
612 label: representation.label,
613 selected: false
614 })
615 }
616
617 const payload: LoadedQualityData = {
618 qualitySwitchCallback: (d: any) => this.qualitySwitchCallback(d),
619 qualityData: {
620 video: qualityLevelsPayload
621 }
622 }
623 this.player.tech(true).trigger('loadedqualitydata', payload)
624 }
625
626 private buildQualityLabel (file: VideoFile) {
627 let label = file.resolution.label
628
629 if (file.fps && file.fps >= 50) {
630 label += file.fps
631 }
632
633 return label
634 }
635
636 private qualitySwitchCallback (id: number) {
637 if (id === -1) {
638 if (this.autoResolutionPossible === true) this.enableAutoResolution()
639 return
640 }
641
642 this.disableAutoResolution()
643 this.updateResolution(id)
644 }
645
646 private changeQuality () {
647 const resolutionId = this.currentVideoFile.resolution.id
648 const qualityLevels = this.player.qualityLevels()
649
650 if (resolutionId === -1) {
651 qualityLevels.selectedIndex = -1
652 return
653 }
654
655 for (let i = 0; i < qualityLevels.length; i++) {
656 const q = qualityLevels[i]
657 if (q.height === resolutionId) qualityLevels.selectedIndex_ = i
658 }
659 }
660 }
661
662 videojs.registerPlugin('webtorrent', WebTorrentPlugin)
663 export { WebTorrentPlugin }