]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-videojs-plugin.ts
Fix #639 providing magnet URI in player and download modal
[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'
7b3a99d5
C
7import { isMobile, videoFileMaxByResolution, videoFileMinByResolution } from './utils'
8import * as CacheChunkStore from 'cache-chunk-store'
9import { PeertubeChunkStore } from './peertube-chunk-store'
6cca7360 10import {
7b3a99d5 11 getAverageBandwidthInStore,
6cca7360 12 getStoredMute,
7b3a99d5 13 getStoredVolume,
6cca7360
C
14 saveAverageBandwidth,
15 saveMuteInStore,
7b3a99d5
C
16 saveVolumeInStore
17} from './peertube-player-local-storage'
be6a4802 18
b7f1747d 19const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin')
a22bfc3e 20class PeerTubePlugin extends Plugin {
c6352f2c 21 private readonly playerElement: HTMLVideoElement
a8462c8e 22
c6352f2c 23 private readonly autoplay: boolean = false
f37bad63 24 private readonly startTime: number = 0
c6352f2c 25 private readonly savePlayerSrcFunction: Function
a8462c8e
C
26 private readonly videoFiles: VideoFile[]
27 private readonly videoViewUrl: string
28 private readonly videoDuration: number
29 private readonly CONSTANTS = {
30 INFO_SCHEDULER: 1000, // Don't change this
31 AUTO_QUALITY_SCHEDULER: 3000, // Check quality every 3 seconds
32 AUTO_QUALITY_THRESHOLD_PERCENT: 30, // Bandwidth should be 30% more important than a resolution bitrate to change to it
7ee4a4af
C
33 AUTO_QUALITY_OBSERVATION_TIME: 10000, // Wait 10 seconds after having change the resolution before another check
34 AUTO_QUALITY_HIGHER_RESOLUTION_DELAY: 5000, // Buffering higher resolution during 5 seconds
a8462c8e
C
35 BANDWIDTH_AVERAGE_NUMBER_OF_VALUES: 5 // Last 5 seconds to build average bandwidth
36 }
37
b7f1747d
C
38 private readonly webtorrent = new WebTorrent({
39 tracker: {
40 rtcConfig: {
41 iceServers: [
42 {
43 urls: 'stun:stun.stunprotocol.org'
44 },
45 {
46 urls: 'stun:stun.framasoft.org'
47 }
48 ]
49 }
50 },
51 dht: false
52 })
53
a22bfc3e
C
54 private player: any
55 private currentVideoFile: VideoFile
a22bfc3e 56 private torrent: WebTorrent.Torrent
b7f1747d 57 private renderer
054a103b 58 private fakeRenderer
7ee4a4af 59 private autoResolution = true
c4082b8b 60 private forbidAutoResolution = false
7ee4a4af
C
61 private isAutoResolutionObservation = false
62
8cac1b64 63 private videoViewInterval
3bcfff7f 64 private torrentInfoInterval
a8462c8e 65 private autoQualityInterval
7ee4a4af
C
66 private addTorrentDelay
67 private qualityObservationTimer
68 private runAutoQualitySchedulerTimer
a8462c8e
C
69
70 private downloadSpeeds: number[] = []
a22bfc3e 71
339632b4 72 constructor (player: videojs.Player, options: PeertubePluginOptions) {
a22bfc3e
C
73 super(player, options)
74
e7eb5b39
C
75 // Disable auto play on iOS
76 this.autoplay = options.autoplay && this.isIOS() === false
481d3596 77
f37bad63 78 this.startTime = options.startTime
a22bfc3e 79 this.videoFiles = options.videoFiles
8cac1b64 80 this.videoViewUrl = options.videoViewUrl
3bcfff7f 81 this.videoDuration = options.videoDuration
a22bfc3e 82
bf5685f0 83 this.savePlayerSrcFunction = this.player.src
a22bfc3e
C
84 // Hack to "simulate" src link in video.js >= 6
85 // Without this, we can't play the video after pausing it
86 // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
bf5685f0 87 this.player.src = () => true
a22bfc3e
C
88
89 this.playerElement = options.playerElement
90
e6f62797
C
91 if (this.autoplay === true) this.player.addClass('vjs-has-autoplay')
92
a22bfc3e 93 this.player.ready(() => {
c6352f2c
C
94 const volume = getStoredVolume()
95 if (volume !== undefined) this.player.volume(volume)
96 const muted = getStoredMute()
97 if (muted !== undefined) this.player.muted(muted)
98
0dcf9a14 99 this.initializePlayer()
a22bfc3e 100 this.runTorrentInfoScheduler()
3bcfff7f 101 this.runViewAdd()
a8462c8e
C
102
103 this.player.one('play', () => {
f37bad63 104 // Don't run immediately scheduler, wait some seconds the TCP connections are made
7ee4a4af
C
105 this.runAutoQualitySchedulerTimer = setTimeout(() => {
106 this.runAutoQualityScheduler()
107 }, this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
a8462c8e 108 })
a22bfc3e 109 })
c6352f2c
C
110
111 this.player.on('volumechange', () => {
112 saveVolumeInStore(this.player.volume())
113 saveMuteInStore(this.player.muted())
114 })
a22bfc3e
C
115 }
116
117 dispose () {
7ee4a4af
C
118 clearTimeout(this.addTorrentDelay)
119 clearTimeout(this.qualityObservationTimer)
120 clearTimeout(this.runAutoQualitySchedulerTimer)
121
3bcfff7f
C
122 clearInterval(this.videoViewInterval)
123 clearInterval(this.torrentInfoInterval)
a8462c8e 124 clearInterval(this.autoQualityInterval)
3bcfff7f 125
a22bfc3e
C
126 // Don't need to destroy renderer, video player will be destroyed
127 this.flushVideoFile(this.currentVideoFile, false)
054a103b
C
128
129 this.destroyFakeRenderer()
aa8b6df4
C
130 }
131
09700934
C
132 getCurrentResolutionId () {
133 return this.currentVideoFile ? this.currentVideoFile.resolution.id : -1
aa8b6df4
C
134 }
135
a22bfc3e 136 getCurrentResolutionLabel () {
09700934 137 return this.currentVideoFile ? this.currentVideoFile.resolution.label : ''
aa8b6df4
C
138 }
139
3baf9be2
C
140 updateVideoFile (
141 videoFile?: VideoFile,
142 options: {
143 forcePlay?: boolean,
144 seek?: number,
145 delay?: number
146 } = {},
147 done: () => void = () => { /* empty */ }
148 ) {
a8462c8e 149 // Automatically choose the adapted video file
aa8b6df4 150 if (videoFile === undefined) {
7b3a99d5 151 const savedAverageBandwidth = getAverageBandwidthInStore()
a8462c8e
C
152 videoFile = savedAverageBandwidth
153 ? this.getAppropriateFile(savedAverageBandwidth)
8eb8bc20 154 : this.pickAverageVideoFile()
aa8b6df4
C
155 }
156
157 // Don't add the same video file once again
a22bfc3e 158 if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) {
aa8b6df4
C
159 return
160 }
161
c6352f2c 162 // Do not display error to user because we will have multiple fallback
bf5685f0 163 this.disableErrorDisplay()
1198a08c 164
bf5685f0 165 this.player.src = () => true
c6352f2c 166 const oldPlaybackRate = this.player.playbackRate()
bf5685f0 167
a22bfc3e
C
168 const previousVideoFile = this.currentVideoFile
169 this.currentVideoFile = videoFile
aa8b6df4 170
3baf9be2 171 this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, options, () => {
c6352f2c
C
172 this.player.playbackRate(oldPlaybackRate)
173 return done()
174 })
a216c623
C
175
176 this.trigger('videoFileUpdate')
177 }
178
3baf9be2
C
179 addTorrent (
180 magnetOrTorrentUrl: string,
181 previousVideoFile: VideoFile,
182 options: {
183 forcePlay?: boolean,
184 seek?: number,
185 delay?: number
186 },
187 done: Function
188 ) {
a216c623
C
189 console.log('Adding ' + magnetOrTorrentUrl + '.')
190
a8462c8e 191 const oldTorrent = this.torrent
3baf9be2 192 const torrentOptions = {
efda99c3
C
193 store: (chunkLength, storeOpts) => new CacheChunkStore(new PeertubeChunkStore(chunkLength, storeOpts), {
194 max: 100
195 })
196 }
197
b7f1747d 198 this.torrent = this.webtorrent.add(magnetOrTorrentUrl, torrentOptions, torrent => {
a216c623 199 console.log('Added ' + magnetOrTorrentUrl + '.')
aa8b6df4 200
a8462c8e 201 if (oldTorrent) {
4a7591e1 202 // Pause the old torrent
a8462c8e
C
203 oldTorrent.pause()
204 // Pause does not remove actual peers (in particular the webseed peer)
205 oldTorrent.removePeer(oldTorrent['ws'])
6d272f39
C
206
207 // We use a fake renderer so we download correct pieces of the next file
6d272f39
C
208 if (options.delay) {
209 const fakeVideoElem = document.createElement('video')
210 renderVideo(torrent.files[0], fakeVideoElem, { autoplay: false, controls: false }, (err, renderer) => {
054a103b 211 this.fakeRenderer = renderer
6d272f39 212
4a7591e1 213 if (err) console.error('Cannot render new torrent in fake video element.', err)
6d272f39
C
214
215 // Load the future file at the correct time
216 fakeVideoElem.currentTime = this.player.currentTime() + (options.delay / 2000)
217 })
218 }
a8462c8e 219 }
aa8b6df4 220
e7eb5b39 221 // Render the video in a few seconds? (on resolution change for example, we wait some seconds of the new video resolution)
7ee4a4af 222 this.addTorrentDelay = setTimeout(() => {
054a103b 223 this.destroyFakeRenderer()
6d272f39 224
3baf9be2
C
225 const paused = this.player.paused()
226
a8462c8e 227 this.flushVideoFile(previousVideoFile)
0dcf9a14 228
3baf9be2
C
229 const renderVideoOptions = { autoplay: false, controls: true }
230 renderVideo(torrent.files[0], this.playerElement, renderVideoOptions,(err, renderer) => {
a8462c8e 231 this.renderer = renderer
aa8b6df4 232
a8462c8e 233 if (err) return this.fallbackToHttp(done)
3bcfff7f 234
3baf9be2
C
235 return this.tryToPlay(err => {
236 if (err) return done(err)
a8462c8e 237
3baf9be2
C
238 if (options.seek) this.seek(options.seek)
239 if (options.forcePlay === false && paused === true) this.player.pause()
240 })
a8462c8e 241 })
3baf9be2 242 }, options.delay || 0)
aa8b6df4
C
243 })
244
a22bfc3e 245 this.torrent.on('error', err => this.handleError(err))
a216c623 246
a22bfc3e 247 this.torrent.on('warning', (err: any) => {
a96aed15 248 // We don't support HTTP tracker but we don't care -> we use the web socket tracker
531ab5b6 249 if (err.message.indexOf('Unsupported tracker protocol') !== -1) return
a216c623 250
7dbdc3ba
C
251 // Users don't care about issues with WebRTC, but developers do so log it in the console
252 if (err.message.indexOf('Ice connection failed') !== -1) {
253 console.error(err)
254 return
255 }
a96aed15 256
a216c623
C
257 // Magnet hash is not up to date with the torrent file, add directly the torrent file
258 if (err.message.indexOf('incorrect info hash') !== -1) {
259 console.error('Incorrect info hash detected, falling back to torrent file.')
3baf9be2
C
260 const options = { forcePlay: true }
261 return this.addTorrent(this.torrent['xs'], previousVideoFile, options, done)
a216c623
C
262 }
263
a22bfc3e 264 return this.handleError(err)
a96aed15 265 })
aa8b6df4
C
266 }
267
a8462c8e 268 updateResolution (resolutionId: number, delay = 0) {
aa8b6df4 269 // Remember player state
a22bfc3e
C
270 const currentTime = this.player.currentTime()
271 const isPaused = this.player.paused()
aa8b6df4 272
531ab5b6
C
273 // Remove poster to have black background
274 this.playerElement.poster = ''
275
aa8b6df4 276 // Hide bigPlayButton
8fa5653a 277 if (!isPaused) {
a22bfc3e 278 this.player.bigPlayButton.hide()
aa8b6df4
C
279 }
280
09700934 281 const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId)
3baf9be2
C
282 const options = {
283 forcePlay: false,
284 delay,
91d95589 285 seek: currentTime + (delay / 1000)
3baf9be2
C
286 }
287 this.updateVideoFile(newVideoFile, options)
aa8b6df4
C
288 }
289
a22bfc3e 290 flushVideoFile (videoFile: VideoFile, destroyRenderer = true) {
b7f1747d 291 if (videoFile !== undefined && this.webtorrent.get(videoFile.magnetUri)) {
bf5685f0
C
292 if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy()
293
b7f1747d 294 this.webtorrent.remove(videoFile.magnetUri)
a22bfc3e 295 console.log('Removed ' + videoFile.magnetUri)
aa8b6df4
C
296 }
297 }
298
a8462c8e
C
299 isAutoResolutionOn () {
300 return this.autoResolution
301 }
302
303 enableAutoResolution () {
304 this.autoResolution = true
305 this.trigger('autoResolutionUpdate')
306 }
307
c4082b8b
C
308 disableAutoResolution (forbid = false) {
309 if (forbid === true) this.forbidAutoResolution = true
310
a8462c8e
C
311 this.autoResolution = false
312 this.trigger('autoResolutionUpdate')
313 }
314
c4082b8b
C
315 isAutoResolutionForbidden () {
316 return this.forbidAutoResolution === true
317 }
318
960a11e8
C
319 getCurrentVideoFile () {
320 return this.currentVideoFile
321 }
322
77728efa
C
323 getTorrent () {
324 return this.torrent
325 }
326
80109b2d
C
327 private tryToPlay (done?: Function) {
328 if (!done) done = function () { /* empty */ }
1fad099d 329
80109b2d
C
330 const playPromise = this.player.play()
331 if (playPromise !== undefined) {
332 return playPromise.then(done)
333 .catch(err => {
334 console.error(err)
335 this.player.pause()
336 this.player.posterImage.show()
337 this.player.removeClass('vjs-has-autoplay')
91d95589 338 this.player.removeClass('vjs-has-big-play-button-clicked')
80109b2d
C
339
340 return done()
341 })
342 }
343
344 return done()
345 }
346
f37bad63
C
347 private seek (time: number) {
348 this.player.currentTime(time)
349 this.player.handleTechSeeked_()
350 }
351
a8462c8e
C
352 private getAppropriateFile (averageDownloadSpeed?: number): VideoFile {
353 if (this.videoFiles === undefined || this.videoFiles.length === 0) return undefined
354 if (this.videoFiles.length === 1) return this.videoFiles[0]
a8462c8e 355
3c40590d
C
356 // Don't change the torrent is the play was ended
357 if (this.torrent && this.torrent.progress === 1 && this.player.ended()) return this.currentVideoFile
358
359 if (!averageDownloadSpeed) averageDownloadSpeed = this.getAndSaveActualDownloadSpeed()
a8462c8e
C
360
361 // Filter videos we can play according to our bandwidth
362 const filteredFiles = this.videoFiles.filter(f => {
363 const fileBitrate = (f.size / this.videoDuration)
364 let threshold = fileBitrate
365
7ee4a4af 366 // If this is for a higher resolution or an initial load: add a margin
a8462c8e
C
367 if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) {
368 threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100)
369 }
370
371 return averageDownloadSpeed > threshold
372 })
373
374 // If the download speed is too bad, return the lowest resolution we have
6cca7360 375 if (filteredFiles.length === 0) return videoFileMinByResolution(this.videoFiles)
a8462c8e 376
6cca7360 377 return videoFileMaxByResolution(filteredFiles)
a8462c8e
C
378 }
379
3c40590d 380 private getAndSaveActualDownloadSpeed () {
a8462c8e
C
381 const start = Math.max(this.downloadSpeeds.length - this.CONSTANTS.BANDWIDTH_AVERAGE_NUMBER_OF_VALUES, 0)
382 const lastDownloadSpeeds = this.downloadSpeeds.slice(start, this.downloadSpeeds.length)
383 if (lastDownloadSpeeds.length === 0) return -1
384
385 const sum = lastDownloadSpeeds.reduce((a, b) => a + b)
386 const averageBandwidth = Math.round(sum / lastDownloadSpeeds.length)
387
388 // Save the average bandwidth for future use
389 saveAverageBandwidth(averageBandwidth)
ed9f9f5f 390
a8462c8e 391 return averageBandwidth
ed9f9f5f
C
392 }
393
0dcf9a14 394 private initializePlayer () {
2ce2fd7f
C
395 if (isMobile()) this.player.addClass('vjs-is-mobile')
396
e993ecb3
C
397 this.initSmoothProgressBar()
398
c6352f2c
C
399 this.alterInactivity()
400
481d3596 401 if (this.autoplay === true) {
33d78552 402 this.player.posterImage.hide()
e6f62797 403
3baf9be2 404 this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime })
aa8b6df4 405 } else {
e7eb5b39
C
406 // Don't try on iOS that does not support MediaSource
407 if (this.isIOS()) {
8eb8bc20 408 this.currentVideoFile = this.pickAverageVideoFile()
e7eb5b39
C
409 return this.fallbackToHttp(undefined, false)
410 }
411
b891f9bc 412 // Proxy first play
c6352f2c
C
413 const oldPlay = this.player.play.bind(this.player)
414 this.player.play = () => {
864e782b 415 this.player.addClass('vjs-has-big-play-button-clicked')
c6352f2c 416 this.player.play = oldPlay
864e782b 417
3baf9be2 418 this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime })
c6352f2c 419 }
aa8b6df4 420 }
a22bfc3e 421 }
aa8b6df4 422
a8462c8e
C
423 private runAutoQualityScheduler () {
424 this.autoQualityInterval = setInterval(() => {
7ee4a4af 425
877b0528
C
426 // Not initialized or in HTTP fallback
427 if (this.torrent === undefined || this.torrent === null) return
a8462c8e
C
428 if (this.isAutoResolutionOn() === false) return
429 if (this.isAutoResolutionObservation === true) return
430
431 const file = this.getAppropriateFile()
432 let changeResolution = false
433 let changeResolutionDelay = 0
434
435 // Lower resolution
436 if (this.isPlayerWaiting() && file.resolution.id < this.currentVideoFile.resolution.id) {
437 console.log('Downgrading automatically the resolution to: %s', file.resolution.label)
438 changeResolution = true
7ee4a4af 439 } else if (file.resolution.id > this.currentVideoFile.resolution.id) { // Higher resolution
a8462c8e
C
440 console.log('Upgrading automatically the resolution to: %s', file.resolution.label)
441 changeResolution = true
7ee4a4af 442 changeResolutionDelay = this.CONSTANTS.AUTO_QUALITY_HIGHER_RESOLUTION_DELAY
a8462c8e
C
443 }
444
445 if (changeResolution === true) {
446 this.updateResolution(file.resolution.id, changeResolutionDelay)
447
448 // Wait some seconds in observation of our new resolution
449 this.isAutoResolutionObservation = true
7ee4a4af
C
450
451 this.qualityObservationTimer = setTimeout(() => {
452 this.isAutoResolutionObservation = false
453 }, this.CONSTANTS.AUTO_QUALITY_OBSERVATION_TIME)
a8462c8e
C
454 }
455 }, this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
456 }
457
458 private isPlayerWaiting () {
7ee4a4af 459 return this.player && this.player.hasClass('vjs-waiting')
a8462c8e
C
460 }
461
a22bfc3e 462 private runTorrentInfoScheduler () {
3bcfff7f 463 this.torrentInfoInterval = setInterval(() => {
bf5685f0
C
464 // Not initialized yet
465 if (this.torrent === undefined) return
466
467 // Http fallback
468 if (this.torrent === null) return this.trigger('torrentInfo', false)
469
b7f1747d
C
470 // this.webtorrent.downloadSpeed because we need to take into account the potential old torrent too
471 if (this.webtorrent.downloadSpeed !== 0) this.downloadSpeeds.push(this.webtorrent.downloadSpeed)
a8462c8e 472
bf5685f0
C
473 return this.trigger('torrentInfo', {
474 downloadSpeed: this.torrent.downloadSpeed,
475 numPeers: this.torrent.numPeers,
1a49822c
C
476 uploadSpeed: this.torrent.uploadSpeed,
477 downloaded: this.torrent.downloaded,
478 uploaded: this.torrent.uploaded
bf5685f0 479 })
a8462c8e 480 }, this.CONSTANTS.INFO_SCHEDULER)
a22bfc3e 481 }
aa8b6df4 482
8cac1b64
C
483 private runViewAdd () {
484 this.clearVideoViewInterval()
485
486 // After 30 seconds (or 3/4 of the video), add a view to the video
487 let minSecondsToView = 30
488
3bcfff7f 489 if (this.videoDuration < minSecondsToView) minSecondsToView = (this.videoDuration * 3) / 4
8cac1b64
C
490
491 let secondsViewed = 0
492 this.videoViewInterval = setInterval(() => {
493 if (this.player && !this.player.paused()) {
494 secondsViewed += 1
495
496 if (secondsViewed > minSecondsToView) {
497 this.clearVideoViewInterval()
498
499 this.addViewToVideo().catch(err => console.error(err))
500 }
501 }
502 }, 1000)
503 }
504
505 private clearVideoViewInterval () {
506 if (this.videoViewInterval !== undefined) {
507 clearInterval(this.videoViewInterval)
508 this.videoViewInterval = undefined
509 }
510 }
511
512 private addViewToVideo () {
f5a2dc48
C
513 if (!this.videoViewUrl) return Promise.resolve(undefined)
514
8cac1b64
C
515 return fetch(this.videoViewUrl, { method: 'POST' })
516 }
517
e7eb5b39 518 private fallbackToHttp (done?: Function, play = true) {
c4082b8b
C
519 this.disableAutoResolution(true)
520
bf5685f0
C
521 this.flushVideoFile(this.currentVideoFile, true)
522 this.torrent = null
523
524 // Enable error display now this is our last fallback
525 this.player.one('error', () => this.enableErrorDisplay())
526
527 const httpUrl = this.currentVideoFile.fileUrl
528 this.player.src = this.savePlayerSrcFunction
529 this.player.src(httpUrl)
e7eb5b39 530 if (play) this.tryToPlay()
c6352f2c 531
e7eb5b39 532 if (done) return done()
bf5685f0
C
533 }
534
a22bfc3e
C
535 private handleError (err: Error | string) {
536 return this.player.trigger('customError', { err })
aa8b6df4 537 }
bf5685f0
C
538
539 private enableErrorDisplay () {
540 this.player.addClass('vjs-error-display-enabled')
541 }
542
543 private disableErrorDisplay () {
544 this.player.removeClass('vjs-error-display-enabled')
545 }
e993ecb3 546
e7eb5b39
C
547 private isIOS () {
548 return !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)
549 }
550
c6352f2c
C
551 private alterInactivity () {
552 let saveInactivityTimeout: number
553
554 const disableInactivity = () => {
555 saveInactivityTimeout = this.player.options_.inactivityTimeout
556 this.player.options_.inactivityTimeout = 0
557 }
558 const enableInactivity = () => {
b891f9bc 559 this.player.options_.inactivityTimeout = saveInactivityTimeout
c6352f2c
C
560 }
561
562 const settingsDialog = this.player.children_.find(c => c.name_ === 'SettingsDialog')
563
564 this.player.controlBar.on('mouseenter', () => disableInactivity())
565 settingsDialog.on('mouseenter', () => disableInactivity())
566 this.player.controlBar.on('mouseleave', () => enableInactivity())
c4082b8b 567 // settingsDialog.on('mouseleave', () => enableInactivity())
c6352f2c
C
568 }
569
8eb8bc20
C
570 private pickAverageVideoFile () {
571 if (this.videoFiles.length === 1) return this.videoFiles[0]
572
573 return this.videoFiles[Math.floor(this.videoFiles.length / 2)]
574 }
575
054a103b
C
576 private destroyFakeRenderer () {
577 if (this.fakeRenderer) {
578 if (this.fakeRenderer.destroy) {
579 try {
580 this.fakeRenderer.destroy()
581 } catch (err) {
582 console.log('Cannot destroy correctly fake renderer.', err)
583 }
584 }
585 this.fakeRenderer = undefined
586 }
587 }
588
e993ecb3
C
589 // Thanks: https://github.com/videojs/video.js/issues/4460#issuecomment-312861657
590 private initSmoothProgressBar () {
591 const SeekBar = videojsUntyped.getComponent('SeekBar')
592 SeekBar.prototype.getPercent = function getPercent () {
593 // Allows for smooth scrubbing, when player can't keep up.
594 // const time = (this.player_.scrubbing()) ?
595 // this.player_.getCache().currentTime :
596 // this.player_.currentTime()
597 const time = this.player_.currentTime()
598 const percent = time / this.player_.duration()
599 return percent >= 1 ? 1 : percent
600 }
601 SeekBar.prototype.handleMouseMove = function handleMouseMove (event) {
602 let newTime = this.calculateDistance(event) * this.player_.duration()
603 if (newTime === this.player_.duration()) {
604 newTime = newTime - 0.1
605 }
606 this.player_.currentTime(newTime)
607 this.update()
608 }
609 }
aa8b6df4 610}
c6352f2c 611
b7f1747d 612videojs.registerPlugin('peertube', PeerTubePlugin)
c6352f2c 613export { PeerTubePlugin }