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