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