]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-videojs-plugin.ts
Fix auto quality with http fallback
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-videojs-plugin.ts
CommitLineData
63c4db6d 1import * as videojs from 'video.js'
aa8b6df4 2import * as WebTorrent from 'webtorrent'
b6827820 3import { VideoFile } from '../../../../shared/models/videos/video.model'
aa8b6df4 4import { renderVideo } from './video-renderer'
c6352f2c
C
5import './settings-menu-button'
6import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
a8462c8e
C
7import {
8 getAverageBandwidth,
9 getStoredMute,
10 getStoredVolume,
11 saveAverageBandwidth,
12 saveMuteInStore,
13 saveVolumeInStore
14} from './utils'
15import minBy from 'lodash-es/minBy'
16import maxBy from 'lodash-es/maxBy'
be6a4802 17
d402fb5b
C
18const webtorrent = new WebTorrent({
19 tracker: {
20 rtcConfig: {
21 iceServers: [
22 {
23 urls: 'stun:stun.stunprotocol.org'
24 },
25 {
26 urls: 'stun:stun.framasoft.org'
d402fb5b
C
27 }
28 ]
29 }
30 },
31 dht: false
32})
aa8b6df4 33
a22bfc3e
C
34const Plugin: VideoJSComponentInterface = videojsUntyped.getPlugin('plugin')
35class PeerTubePlugin extends Plugin {
c6352f2c 36 private readonly playerElement: HTMLVideoElement
a8462c8e 37
c6352f2c
C
38 private readonly autoplay: boolean = false
39 private readonly savePlayerSrcFunction: Function
a8462c8e
C
40 private readonly videoFiles: VideoFile[]
41 private readonly videoViewUrl: string
42 private readonly videoDuration: number
43 private readonly CONSTANTS = {
44 INFO_SCHEDULER: 1000, // Don't change this
45 AUTO_QUALITY_SCHEDULER: 3000, // Check quality every 3 seconds
46 AUTO_QUALITY_THRESHOLD_PERCENT: 30, // Bandwidth should be 30% more important than a resolution bitrate to change to it
47 AUTO_QUALITY_OBSERVATION_TIME: 10000, // Wait 10 seconds before potentially changing the definition
48 AUTO_QUALITY_UPPER_RESOLUTION_DELAY: 5000, // Buffer upper resolution during 5 seconds
49 BANDWIDTH_AVERAGE_NUMBER_OF_VALUES: 5 // Last 5 seconds to build average bandwidth
50 }
51
a22bfc3e
C
52 private player: any
53 private currentVideoFile: VideoFile
a22bfc3e 54 private torrent: WebTorrent.Torrent
8cac1b64 55 private videoViewInterval
3bcfff7f 56 private torrentInfoInterval
a8462c8e
C
57 private autoQualityInterval
58 private autoResolution = true
59 private isAutoResolutionObservation = false
60
61 private downloadSpeeds: number[] = []
a22bfc3e 62
339632b4 63 constructor (player: videojs.Player, options: PeertubePluginOptions) {
a22bfc3e
C
64 super(player, options)
65
481d3596
C
66 // Fix canplay event on google chrome by disabling default videojs autoplay
67 this.autoplay = this.player.options_.autoplay
68 this.player.options_.autoplay = false
69
a22bfc3e 70 this.videoFiles = options.videoFiles
8cac1b64 71 this.videoViewUrl = options.videoViewUrl
3bcfff7f 72 this.videoDuration = options.videoDuration
a22bfc3e 73
bf5685f0 74 this.savePlayerSrcFunction = this.player.src
a22bfc3e
C
75 // Hack to "simulate" src link in video.js >= 6
76 // Without this, we can't play the video after pausing it
77 // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
bf5685f0 78 this.player.src = () => true
a22bfc3e
C
79
80 this.playerElement = options.playerElement
81
82 this.player.ready(() => {
c6352f2c
C
83 const volume = getStoredVolume()
84 if (volume !== undefined) this.player.volume(volume)
85 const muted = getStoredMute()
86 if (muted !== undefined) this.player.muted(muted)
87
0dcf9a14 88 this.initializePlayer()
a22bfc3e 89 this.runTorrentInfoScheduler()
3bcfff7f 90 this.runViewAdd()
a8462c8e
C
91
92 this.player.one('play', () => {
93 // Don't run immediately scheduler, wait some seconds the TCP connections are maid
94 setTimeout(() => this.runAutoQualityScheduler(), this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
95 })
a22bfc3e 96 })
c6352f2c
C
97
98 this.player.on('volumechange', () => {
99 saveVolumeInStore(this.player.volume())
100 saveMuteInStore(this.player.muted())
101 })
a22bfc3e
C
102 }
103
104 dispose () {
3bcfff7f
C
105 clearInterval(this.videoViewInterval)
106 clearInterval(this.torrentInfoInterval)
a8462c8e 107 clearInterval(this.autoQualityInterval)
3bcfff7f 108
a22bfc3e
C
109 // Don't need to destroy renderer, video player will be destroyed
110 this.flushVideoFile(this.currentVideoFile, false)
aa8b6df4
C
111 }
112
09700934
C
113 getCurrentResolutionId () {
114 return this.currentVideoFile ? this.currentVideoFile.resolution.id : -1
aa8b6df4
C
115 }
116
a22bfc3e 117 getCurrentResolutionLabel () {
09700934 118 return this.currentVideoFile ? this.currentVideoFile.resolution.label : ''
aa8b6df4
C
119 }
120
a8462c8e 121 updateVideoFile (videoFile?: VideoFile, delay = 0, done?: () => void) {
aa8b6df4
C
122 if (done === undefined) {
123 done = () => { /* empty */ }
124 }
125
a8462c8e 126 // Automatically choose the adapted video file
aa8b6df4 127 if (videoFile === undefined) {
a8462c8e
C
128 const savedAverageBandwidth = getAverageBandwidth()
129 videoFile = savedAverageBandwidth
130 ? this.getAppropriateFile(savedAverageBandwidth)
131 : this.videoFiles[0]
aa8b6df4
C
132 }
133
134 // Don't add the same video file once again
a22bfc3e 135 if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) {
aa8b6df4
C
136 return
137 }
138
c6352f2c 139 // Do not display error to user because we will have multiple fallback
bf5685f0 140 this.disableErrorDisplay()
1198a08c 141
bf5685f0 142 this.player.src = () => true
c6352f2c 143 const oldPlaybackRate = this.player.playbackRate()
bf5685f0 144
a22bfc3e
C
145 const previousVideoFile = this.currentVideoFile
146 this.currentVideoFile = videoFile
aa8b6df4 147
a8462c8e 148 this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, delay, () => {
c6352f2c
C
149 this.player.playbackRate(oldPlaybackRate)
150 return done()
151 })
a216c623
C
152
153 this.trigger('videoFileUpdate')
154 }
155
a8462c8e 156 addTorrent (magnetOrTorrentUrl: string, previousVideoFile: VideoFile, delay = 0, done: Function) {
a216c623
C
157 console.log('Adding ' + magnetOrTorrentUrl + '.')
158
a8462c8e 159 const oldTorrent = this.torrent
a216c623
C
160 this.torrent = webtorrent.add(magnetOrTorrentUrl, torrent => {
161 console.log('Added ' + magnetOrTorrentUrl + '.')
aa8b6df4 162
a8462c8e
C
163 // Pause the old torrent
164 if (oldTorrent) {
165 oldTorrent.pause()
166 // Pause does not remove actual peers (in particular the webseed peer)
167 oldTorrent.removePeer(oldTorrent['ws'])
168 }
aa8b6df4 169
a8462c8e
C
170 setTimeout(() => {
171 this.flushVideoFile(previousVideoFile)
0dcf9a14 172
a8462c8e
C
173 const options = { autoplay: true, controls: true }
174 renderVideo(torrent.files[0], this.playerElement, options,(err, renderer) => {
175 this.renderer = renderer
aa8b6df4 176
a8462c8e 177 if (err) return this.fallbackToHttp(done)
3bcfff7f 178
a8462c8e
C
179 if (!this.player.paused()) {
180 const playPromise = this.player.play()
181 if (playPromise !== undefined) return playPromise.then(done)
3bcfff7f 182
a8462c8e
C
183 return done()
184 }
185
186 return done()
187 })
188 }, delay)
aa8b6df4
C
189 })
190
a22bfc3e 191 this.torrent.on('error', err => this.handleError(err))
a216c623 192
a22bfc3e 193 this.torrent.on('warning', (err: any) => {
a96aed15 194 // We don't support HTTP tracker but we don't care -> we use the web socket tracker
531ab5b6 195 if (err.message.indexOf('Unsupported tracker protocol') !== -1) return
a216c623 196
7dbdc3ba
C
197 // Users don't care about issues with WebRTC, but developers do so log it in the console
198 if (err.message.indexOf('Ice connection failed') !== -1) {
199 console.error(err)
200 return
201 }
a96aed15 202
a216c623
C
203 // Magnet hash is not up to date with the torrent file, add directly the torrent file
204 if (err.message.indexOf('incorrect info hash') !== -1) {
205 console.error('Incorrect info hash detected, falling back to torrent file.')
a8462c8e 206 return this.addTorrent(this.torrent['xs'], previousVideoFile, 0, done)
a216c623
C
207 }
208
a22bfc3e 209 return this.handleError(err)
a96aed15 210 })
aa8b6df4
C
211 }
212
a8462c8e 213 updateResolution (resolutionId: number, delay = 0) {
aa8b6df4 214 // Remember player state
a22bfc3e
C
215 const currentTime = this.player.currentTime()
216 const isPaused = this.player.paused()
aa8b6df4 217
531ab5b6
C
218 // Remove poster to have black background
219 this.playerElement.poster = ''
220
aa8b6df4 221 // Hide bigPlayButton
8fa5653a 222 if (!isPaused) {
a22bfc3e 223 this.player.bigPlayButton.hide()
aa8b6df4
C
224 }
225
09700934 226 const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId)
a8462c8e 227 this.updateVideoFile(newVideoFile, delay, () => {
a22bfc3e
C
228 this.player.currentTime(currentTime)
229 this.player.handleTechSeeked_()
aa8b6df4
C
230 })
231 }
232
a22bfc3e 233 flushVideoFile (videoFile: VideoFile, destroyRenderer = true) {
aa8b6df4 234 if (videoFile !== undefined && webtorrent.get(videoFile.magnetUri)) {
bf5685f0
C
235 if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy()
236
aa8b6df4 237 webtorrent.remove(videoFile.magnetUri)
a22bfc3e 238 console.log('Removed ' + videoFile.magnetUri)
aa8b6df4
C
239 }
240 }
241
a8462c8e
C
242 isAutoResolutionOn () {
243 return this.autoResolution
244 }
245
246 enableAutoResolution () {
247 this.autoResolution = true
248 this.trigger('autoResolutionUpdate')
249 }
250
251 disableAutoResolution () {
252 this.autoResolution = false
253 this.trigger('autoResolutionUpdate')
254 }
255
256 private getAppropriateFile (averageDownloadSpeed?: number): VideoFile {
257 if (this.videoFiles === undefined || this.videoFiles.length === 0) return undefined
258 if (this.videoFiles.length === 1) return this.videoFiles[0]
259 if (this.torrent && this.torrent.progress === 1) return this.currentVideoFile
260
261 if (!averageDownloadSpeed) averageDownloadSpeed = this.getActualDownloadSpeed()
262
263 // Filter videos we can play according to our bandwidth
264 const filteredFiles = this.videoFiles.filter(f => {
265 const fileBitrate = (f.size / this.videoDuration)
266 let threshold = fileBitrate
267
268 // If this is for a higher resolution, or an initial load -> add a upper margin
269 if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) {
270 threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100)
271 }
272
273 return averageDownloadSpeed > threshold
274 })
275
276 // If the download speed is too bad, return the lowest resolution we have
277 if (filteredFiles.length === 0) return minBy(this.videoFiles, 'resolution.id')
278
279 return maxBy(filteredFiles, 'resolution.id')
280 }
281
282 private getActualDownloadSpeed () {
283 const start = Math.max(this.downloadSpeeds.length - this.CONSTANTS.BANDWIDTH_AVERAGE_NUMBER_OF_VALUES, 0)
284 const lastDownloadSpeeds = this.downloadSpeeds.slice(start, this.downloadSpeeds.length)
285 if (lastDownloadSpeeds.length === 0) return -1
286
287 const sum = lastDownloadSpeeds.reduce((a, b) => a + b)
288 const averageBandwidth = Math.round(sum / lastDownloadSpeeds.length)
289
290 // Save the average bandwidth for future use
291 saveAverageBandwidth(averageBandwidth)
ed9f9f5f 292
a8462c8e 293 return averageBandwidth
ed9f9f5f
C
294 }
295
0dcf9a14 296 private initializePlayer () {
e993ecb3
C
297 this.initSmoothProgressBar()
298
c6352f2c
C
299 this.alterInactivity()
300
481d3596 301 if (this.autoplay === true) {
33d78552 302 this.player.posterImage.hide()
a8462c8e 303 this.updateVideoFile(undefined, 0, () => this.player.play())
aa8b6df4 304 } else {
b891f9bc 305 // Proxy first play
c6352f2c
C
306 const oldPlay = this.player.play.bind(this.player)
307 this.player.play = () => {
a8462c8e 308 this.updateVideoFile(undefined, 0, () => oldPlay)
c6352f2c
C
309 this.player.play = oldPlay
310 }
aa8b6df4 311 }
a22bfc3e 312 }
aa8b6df4 313
a8462c8e
C
314 private runAutoQualityScheduler () {
315 this.autoQualityInterval = setInterval(() => {
877b0528
C
316 // Not initialized or in HTTP fallback
317 if (this.torrent === undefined || this.torrent === null) return
a8462c8e
C
318 if (this.isAutoResolutionOn() === false) return
319 if (this.isAutoResolutionObservation === true) return
320
321 const file = this.getAppropriateFile()
322 let changeResolution = false
323 let changeResolutionDelay = 0
324
325 // Lower resolution
326 if (this.isPlayerWaiting() && file.resolution.id < this.currentVideoFile.resolution.id) {
327 console.log('Downgrading automatically the resolution to: %s', file.resolution.label)
328 changeResolution = true
329 } else if (file.resolution.id > this.currentVideoFile.resolution.id) { // Greater resolution
330 console.log('Upgrading automatically the resolution to: %s', file.resolution.label)
331 changeResolution = true
332 changeResolutionDelay = this.CONSTANTS.AUTO_QUALITY_UPPER_RESOLUTION_DELAY
333 }
334
335 if (changeResolution === true) {
336 this.updateResolution(file.resolution.id, changeResolutionDelay)
337
338 // Wait some seconds in observation of our new resolution
339 this.isAutoResolutionObservation = true
340 setTimeout(() => this.isAutoResolutionObservation = false, this.CONSTANTS.AUTO_QUALITY_OBSERVATION_TIME)
341 }
342 }, this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
343 }
344
345 private isPlayerWaiting () {
346 return this.player.hasClass('vjs-waiting')
347 }
348
a22bfc3e 349 private runTorrentInfoScheduler () {
3bcfff7f 350 this.torrentInfoInterval = setInterval(() => {
bf5685f0
C
351 // Not initialized yet
352 if (this.torrent === undefined) return
353
354 // Http fallback
355 if (this.torrent === null) return this.trigger('torrentInfo', false)
356
a8462c8e
C
357 // webtorrent.downloadSpeed because we need to take into account the potential old torrent too
358 if (webtorrent.downloadSpeed !== 0) this.downloadSpeeds.push(webtorrent.downloadSpeed)
359
bf5685f0
C
360 return this.trigger('torrentInfo', {
361 downloadSpeed: this.torrent.downloadSpeed,
362 numPeers: this.torrent.numPeers,
363 uploadSpeed: this.torrent.uploadSpeed
364 })
a8462c8e 365 }, this.CONSTANTS.INFO_SCHEDULER)
a22bfc3e 366 }
aa8b6df4 367
8cac1b64
C
368 private runViewAdd () {
369 this.clearVideoViewInterval()
370
371 // After 30 seconds (or 3/4 of the video), add a view to the video
372 let minSecondsToView = 30
373
3bcfff7f 374 if (this.videoDuration < minSecondsToView) minSecondsToView = (this.videoDuration * 3) / 4
8cac1b64
C
375
376 let secondsViewed = 0
377 this.videoViewInterval = setInterval(() => {
378 if (this.player && !this.player.paused()) {
379 secondsViewed += 1
380
381 if (secondsViewed > minSecondsToView) {
382 this.clearVideoViewInterval()
383
384 this.addViewToVideo().catch(err => console.error(err))
385 }
386 }
387 }, 1000)
388 }
389
390 private clearVideoViewInterval () {
391 if (this.videoViewInterval !== undefined) {
392 clearInterval(this.videoViewInterval)
393 this.videoViewInterval = undefined
394 }
395 }
396
397 private addViewToVideo () {
398 return fetch(this.videoViewUrl, { method: 'POST' })
399 }
400
c6352f2c 401 private fallbackToHttp (done: Function) {
bf5685f0
C
402 this.flushVideoFile(this.currentVideoFile, true)
403 this.torrent = null
404
405 // Enable error display now this is our last fallback
406 this.player.one('error', () => this.enableErrorDisplay())
407
408 const httpUrl = this.currentVideoFile.fileUrl
409 this.player.src = this.savePlayerSrcFunction
410 this.player.src(httpUrl)
411 this.player.play()
c6352f2c
C
412
413 return done()
bf5685f0
C
414 }
415
a22bfc3e
C
416 private handleError (err: Error | string) {
417 return this.player.trigger('customError', { err })
aa8b6df4 418 }
bf5685f0
C
419
420 private enableErrorDisplay () {
421 this.player.addClass('vjs-error-display-enabled')
422 }
423
424 private disableErrorDisplay () {
425 this.player.removeClass('vjs-error-display-enabled')
426 }
e993ecb3 427
c6352f2c
C
428 private alterInactivity () {
429 let saveInactivityTimeout: number
430
431 const disableInactivity = () => {
432 saveInactivityTimeout = this.player.options_.inactivityTimeout
433 this.player.options_.inactivityTimeout = 0
434 }
435 const enableInactivity = () => {
b891f9bc 436 this.player.options_.inactivityTimeout = saveInactivityTimeout
c6352f2c
C
437 }
438
439 const settingsDialog = this.player.children_.find(c => c.name_ === 'SettingsDialog')
440
441 this.player.controlBar.on('mouseenter', () => disableInactivity())
442 settingsDialog.on('mouseenter', () => disableInactivity())
443 this.player.controlBar.on('mouseleave', () => enableInactivity())
444 settingsDialog.on('mouseleave', () => enableInactivity())
445 }
446
e993ecb3
C
447 // Thanks: https://github.com/videojs/video.js/issues/4460#issuecomment-312861657
448 private initSmoothProgressBar () {
449 const SeekBar = videojsUntyped.getComponent('SeekBar')
450 SeekBar.prototype.getPercent = function getPercent () {
451 // Allows for smooth scrubbing, when player can't keep up.
452 // const time = (this.player_.scrubbing()) ?
453 // this.player_.getCache().currentTime :
454 // this.player_.currentTime()
455 const time = this.player_.currentTime()
456 const percent = time / this.player_.duration()
457 return percent >= 1 ? 1 : percent
458 }
459 SeekBar.prototype.handleMouseMove = function handleMouseMove (event) {
460 let newTime = this.calculateDistance(event) * this.player_.duration()
461 if (newTime === this.player_.duration()) {
462 newTime = newTime - 0.1
463 }
464 this.player_.currentTime(newTime)
465 this.update()
466 }
467 }
aa8b6df4 468}
c6352f2c 469
a22bfc3e 470videojsUntyped.registerPlugin('peertube', PeerTubePlugin)
c6352f2c 471export { PeerTubePlugin }