]>
Commit | Line | Data |
---|---|---|
63c4db6d | 1 | import * as videojs from 'video.js' |
aa8b6df4 | 2 | import * as WebTorrent from 'webtorrent' |
b6827820 | 3 | import { VideoFile } from '../../../../shared/models/videos/video.model' |
aa8b6df4 | 4 | import { renderVideo } from './video-renderer' |
c6352f2c C |
5 | import './settings-menu-button' |
6 | import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' | |
d1bd87e0 C |
7 | import { |
8 | getAverageBandwidth, | |
9 | getStoredMute, | |
10 | getStoredVolume, | |
11 | isMobile, | |
12 | saveAverageBandwidth, | |
13 | saveMuteInStore, | |
14 | saveVolumeInStore | |
15 | } from './utils' | |
a8462c8e C |
16 | import minBy from 'lodash-es/minBy' |
17 | import maxBy from 'lodash-es/maxBy' | |
efda99c3 C |
18 | import * as CacheChunkStore from 'cache-chunk-store' |
19 | import { PeertubeChunkStore } from './peertube-chunk-store' | |
be6a4802 | 20 | |
d402fb5b C |
21 | const webtorrent = new WebTorrent({ |
22 | tracker: { | |
23 | rtcConfig: { | |
24 | iceServers: [ | |
25 | { | |
26 | urls: 'stun:stun.stunprotocol.org' | |
27 | }, | |
28 | { | |
29 | urls: 'stun:stun.framasoft.org' | |
d402fb5b C |
30 | } |
31 | ] | |
32 | } | |
33 | }, | |
34 | dht: false | |
35 | }) | |
aa8b6df4 | 36 | |
a22bfc3e C |
37 | const Plugin: VideoJSComponentInterface = videojsUntyped.getPlugin('plugin') |
38 | class PeerTubePlugin extends Plugin { | |
c6352f2c | 39 | private readonly playerElement: HTMLVideoElement |
a8462c8e | 40 | |
c6352f2c | 41 | private readonly autoplay: boolean = false |
f37bad63 | 42 | private readonly startTime: number = 0 |
c6352f2c | 43 | private readonly savePlayerSrcFunction: Function |
a8462c8e C |
44 | private readonly videoFiles: VideoFile[] |
45 | private readonly videoViewUrl: string | |
46 | private readonly videoDuration: number | |
47 | private readonly CONSTANTS = { | |
48 | INFO_SCHEDULER: 1000, // Don't change this | |
49 | AUTO_QUALITY_SCHEDULER: 3000, // Check quality every 3 seconds | |
50 | AUTO_QUALITY_THRESHOLD_PERCENT: 30, // Bandwidth should be 30% more important than a resolution bitrate to change to it | |
7ee4a4af C |
51 | AUTO_QUALITY_OBSERVATION_TIME: 10000, // Wait 10 seconds after having change the resolution before another check |
52 | AUTO_QUALITY_HIGHER_RESOLUTION_DELAY: 5000, // Buffering higher resolution during 5 seconds | |
a8462c8e C |
53 | BANDWIDTH_AVERAGE_NUMBER_OF_VALUES: 5 // Last 5 seconds to build average bandwidth |
54 | } | |
55 | ||
a22bfc3e C |
56 | private player: any |
57 | private currentVideoFile: VideoFile | |
a22bfc3e | 58 | private torrent: WebTorrent.Torrent |
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) | |
aa8b6df4 C |
127 | } |
128 | ||
09700934 C |
129 | getCurrentResolutionId () { |
130 | return this.currentVideoFile ? this.currentVideoFile.resolution.id : -1 | |
aa8b6df4 C |
131 | } |
132 | ||
a22bfc3e | 133 | getCurrentResolutionLabel () { |
09700934 | 134 | return this.currentVideoFile ? this.currentVideoFile.resolution.label : '' |
aa8b6df4 C |
135 | } |
136 | ||
a8462c8e | 137 | updateVideoFile (videoFile?: VideoFile, delay = 0, done?: () => void) { |
aa8b6df4 C |
138 | if (done === undefined) { |
139 | done = () => { /* empty */ } | |
140 | } | |
141 | ||
a8462c8e | 142 | // Automatically choose the adapted video file |
aa8b6df4 | 143 | if (videoFile === undefined) { |
a8462c8e C |
144 | const savedAverageBandwidth = getAverageBandwidth() |
145 | videoFile = savedAverageBandwidth | |
146 | ? this.getAppropriateFile(savedAverageBandwidth) | |
147 | : this.videoFiles[0] | |
aa8b6df4 C |
148 | } |
149 | ||
150 | // Don't add the same video file once again | |
a22bfc3e | 151 | if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) { |
aa8b6df4 C |
152 | return |
153 | } | |
154 | ||
c6352f2c | 155 | // Do not display error to user because we will have multiple fallback |
bf5685f0 | 156 | this.disableErrorDisplay() |
1198a08c | 157 | |
bf5685f0 | 158 | this.player.src = () => true |
c6352f2c | 159 | const oldPlaybackRate = this.player.playbackRate() |
bf5685f0 | 160 | |
a22bfc3e C |
161 | const previousVideoFile = this.currentVideoFile |
162 | this.currentVideoFile = videoFile | |
aa8b6df4 | 163 | |
a8462c8e | 164 | this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, delay, () => { |
c6352f2c C |
165 | this.player.playbackRate(oldPlaybackRate) |
166 | return done() | |
167 | }) | |
a216c623 C |
168 | |
169 | this.trigger('videoFileUpdate') | |
170 | } | |
171 | ||
a8462c8e | 172 | addTorrent (magnetOrTorrentUrl: string, previousVideoFile: VideoFile, delay = 0, done: Function) { |
a216c623 C |
173 | console.log('Adding ' + magnetOrTorrentUrl + '.') |
174 | ||
a8462c8e | 175 | const oldTorrent = this.torrent |
efda99c3 C |
176 | const options = { |
177 | store: (chunkLength, storeOpts) => new CacheChunkStore(new PeertubeChunkStore(chunkLength, storeOpts), { | |
178 | max: 100 | |
179 | }) | |
180 | } | |
181 | ||
182 | this.torrent = webtorrent.add(magnetOrTorrentUrl, options, torrent => { | |
a216c623 | 183 | console.log('Added ' + magnetOrTorrentUrl + '.') |
aa8b6df4 | 184 | |
a8462c8e C |
185 | // Pause the old torrent |
186 | if (oldTorrent) { | |
187 | oldTorrent.pause() | |
188 | // Pause does not remove actual peers (in particular the webseed peer) | |
189 | oldTorrent.removePeer(oldTorrent['ws']) | |
190 | } | |
aa8b6df4 | 191 | |
e7eb5b39 | 192 | // Render the video in a few seconds? (on resolution change for example, we wait some seconds of the new video resolution) |
7ee4a4af | 193 | this.addTorrentDelay = setTimeout(() => { |
a8462c8e | 194 | this.flushVideoFile(previousVideoFile) |
0dcf9a14 | 195 | |
a8462c8e C |
196 | const options = { autoplay: true, controls: true } |
197 | renderVideo(torrent.files[0], this.playerElement, options,(err, renderer) => { | |
198 | this.renderer = renderer | |
aa8b6df4 | 199 | |
a8462c8e | 200 | if (err) return this.fallbackToHttp(done) |
3bcfff7f | 201 | |
80109b2d | 202 | if (!this.player.paused()) return this.tryToPlay(done) |
a8462c8e C |
203 | |
204 | return done() | |
205 | }) | |
206 | }, delay) | |
aa8b6df4 C |
207 | }) |
208 | ||
a22bfc3e | 209 | this.torrent.on('error', err => this.handleError(err)) |
a216c623 | 210 | |
a22bfc3e | 211 | this.torrent.on('warning', (err: any) => { |
a96aed15 | 212 | // We don't support HTTP tracker but we don't care -> we use the web socket tracker |
531ab5b6 | 213 | if (err.message.indexOf('Unsupported tracker protocol') !== -1) return |
a216c623 | 214 | |
7dbdc3ba C |
215 | // Users don't care about issues with WebRTC, but developers do so log it in the console |
216 | if (err.message.indexOf('Ice connection failed') !== -1) { | |
217 | console.error(err) | |
218 | return | |
219 | } | |
a96aed15 | 220 | |
a216c623 C |
221 | // Magnet hash is not up to date with the torrent file, add directly the torrent file |
222 | if (err.message.indexOf('incorrect info hash') !== -1) { | |
223 | console.error('Incorrect info hash detected, falling back to torrent file.') | |
a8462c8e | 224 | return this.addTorrent(this.torrent['xs'], previousVideoFile, 0, done) |
a216c623 C |
225 | } |
226 | ||
a22bfc3e | 227 | return this.handleError(err) |
a96aed15 | 228 | }) |
aa8b6df4 C |
229 | } |
230 | ||
a8462c8e | 231 | updateResolution (resolutionId: number, delay = 0) { |
aa8b6df4 | 232 | // Remember player state |
a22bfc3e C |
233 | const currentTime = this.player.currentTime() |
234 | const isPaused = this.player.paused() | |
aa8b6df4 | 235 | |
531ab5b6 C |
236 | // Remove poster to have black background |
237 | this.playerElement.poster = '' | |
238 | ||
aa8b6df4 | 239 | // Hide bigPlayButton |
8fa5653a | 240 | if (!isPaused) { |
a22bfc3e | 241 | this.player.bigPlayButton.hide() |
aa8b6df4 C |
242 | } |
243 | ||
09700934 | 244 | const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId) |
f37bad63 | 245 | this.updateVideoFile(newVideoFile, delay, () => this.seek(currentTime)) |
aa8b6df4 C |
246 | } |
247 | ||
a22bfc3e | 248 | flushVideoFile (videoFile: VideoFile, destroyRenderer = true) { |
aa8b6df4 | 249 | if (videoFile !== undefined && webtorrent.get(videoFile.magnetUri)) { |
bf5685f0 C |
250 | if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy() |
251 | ||
aa8b6df4 | 252 | webtorrent.remove(videoFile.magnetUri) |
a22bfc3e | 253 | console.log('Removed ' + videoFile.magnetUri) |
aa8b6df4 C |
254 | } |
255 | } | |
256 | ||
a8462c8e C |
257 | isAutoResolutionOn () { |
258 | return this.autoResolution | |
259 | } | |
260 | ||
261 | enableAutoResolution () { | |
262 | this.autoResolution = true | |
263 | this.trigger('autoResolutionUpdate') | |
264 | } | |
265 | ||
266 | disableAutoResolution () { | |
267 | this.autoResolution = false | |
268 | this.trigger('autoResolutionUpdate') | |
269 | } | |
270 | ||
960a11e8 C |
271 | getCurrentVideoFile () { |
272 | return this.currentVideoFile | |
273 | } | |
274 | ||
77728efa C |
275 | getTorrent () { |
276 | return this.torrent | |
277 | } | |
278 | ||
80109b2d C |
279 | private tryToPlay (done?: Function) { |
280 | if (!done) done = function () { /* empty */ } | |
1fad099d | 281 | |
80109b2d C |
282 | const playPromise = this.player.play() |
283 | if (playPromise !== undefined) { | |
284 | return playPromise.then(done) | |
285 | .catch(err => { | |
286 | console.error(err) | |
287 | this.player.pause() | |
288 | this.player.posterImage.show() | |
289 | this.player.removeClass('vjs-has-autoplay') | |
290 | ||
291 | return done() | |
292 | }) | |
293 | } | |
294 | ||
295 | return done() | |
296 | } | |
297 | ||
f37bad63 C |
298 | private seek (time: number) { |
299 | this.player.currentTime(time) | |
300 | this.player.handleTechSeeked_() | |
301 | } | |
302 | ||
a8462c8e C |
303 | private getAppropriateFile (averageDownloadSpeed?: number): VideoFile { |
304 | if (this.videoFiles === undefined || this.videoFiles.length === 0) return undefined | |
305 | if (this.videoFiles.length === 1) return this.videoFiles[0] | |
a8462c8e | 306 | |
3c40590d C |
307 | // Don't change the torrent is the play was ended |
308 | if (this.torrent && this.torrent.progress === 1 && this.player.ended()) return this.currentVideoFile | |
309 | ||
310 | if (!averageDownloadSpeed) averageDownloadSpeed = this.getAndSaveActualDownloadSpeed() | |
a8462c8e C |
311 | |
312 | // Filter videos we can play according to our bandwidth | |
313 | const filteredFiles = this.videoFiles.filter(f => { | |
314 | const fileBitrate = (f.size / this.videoDuration) | |
315 | let threshold = fileBitrate | |
316 | ||
7ee4a4af | 317 | // If this is for a higher resolution or an initial load: add a margin |
a8462c8e C |
318 | if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) { |
319 | threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100) | |
320 | } | |
321 | ||
322 | return averageDownloadSpeed > threshold | |
323 | }) | |
324 | ||
325 | // If the download speed is too bad, return the lowest resolution we have | |
326 | if (filteredFiles.length === 0) return minBy(this.videoFiles, 'resolution.id') | |
327 | ||
328 | return maxBy(filteredFiles, 'resolution.id') | |
329 | } | |
330 | ||
3c40590d | 331 | private getAndSaveActualDownloadSpeed () { |
a8462c8e C |
332 | const start = Math.max(this.downloadSpeeds.length - this.CONSTANTS.BANDWIDTH_AVERAGE_NUMBER_OF_VALUES, 0) |
333 | const lastDownloadSpeeds = this.downloadSpeeds.slice(start, this.downloadSpeeds.length) | |
334 | if (lastDownloadSpeeds.length === 0) return -1 | |
335 | ||
336 | const sum = lastDownloadSpeeds.reduce((a, b) => a + b) | |
337 | const averageBandwidth = Math.round(sum / lastDownloadSpeeds.length) | |
338 | ||
339 | // Save the average bandwidth for future use | |
340 | saveAverageBandwidth(averageBandwidth) | |
ed9f9f5f | 341 | |
a8462c8e | 342 | return averageBandwidth |
ed9f9f5f C |
343 | } |
344 | ||
0dcf9a14 | 345 | private initializePlayer () { |
e993ecb3 C |
346 | this.initSmoothProgressBar() |
347 | ||
c6352f2c C |
348 | this.alterInactivity() |
349 | ||
481d3596 | 350 | if (this.autoplay === true) { |
33d78552 | 351 | this.player.posterImage.hide() |
e6f62797 | 352 | |
80109b2d C |
353 | this.updateVideoFile(undefined, 0, () => { |
354 | this.seek(this.startTime) | |
355 | this.tryToPlay() | |
356 | }) | |
aa8b6df4 | 357 | } else { |
e7eb5b39 C |
358 | // Don't try on iOS that does not support MediaSource |
359 | if (this.isIOS()) { | |
360 | this.currentVideoFile = this.videoFiles[0] | |
361 | return this.fallbackToHttp(undefined, false) | |
362 | } | |
363 | ||
b891f9bc | 364 | // Proxy first play |
c6352f2c C |
365 | const oldPlay = this.player.play.bind(this.player) |
366 | this.player.play = () => { | |
864e782b | 367 | this.player.addClass('vjs-has-big-play-button-clicked') |
c6352f2c | 368 | this.player.play = oldPlay |
864e782b C |
369 | |
370 | this.updateVideoFile(undefined, 0, () => this.seek(this.startTime)) | |
c6352f2c | 371 | } |
aa8b6df4 | 372 | } |
a22bfc3e | 373 | } |
aa8b6df4 | 374 | |
a8462c8e C |
375 | private runAutoQualityScheduler () { |
376 | this.autoQualityInterval = setInterval(() => { | |
7ee4a4af | 377 | |
877b0528 C |
378 | // Not initialized or in HTTP fallback |
379 | if (this.torrent === undefined || this.torrent === null) return | |
a8462c8e C |
380 | if (this.isAutoResolutionOn() === false) return |
381 | if (this.isAutoResolutionObservation === true) return | |
382 | ||
383 | const file = this.getAppropriateFile() | |
384 | let changeResolution = false | |
385 | let changeResolutionDelay = 0 | |
386 | ||
387 | // Lower resolution | |
388 | if (this.isPlayerWaiting() && file.resolution.id < this.currentVideoFile.resolution.id) { | |
389 | console.log('Downgrading automatically the resolution to: %s', file.resolution.label) | |
390 | changeResolution = true | |
7ee4a4af | 391 | } else if (file.resolution.id > this.currentVideoFile.resolution.id) { // Higher resolution |
a8462c8e C |
392 | console.log('Upgrading automatically the resolution to: %s', file.resolution.label) |
393 | changeResolution = true | |
7ee4a4af | 394 | changeResolutionDelay = this.CONSTANTS.AUTO_QUALITY_HIGHER_RESOLUTION_DELAY |
a8462c8e C |
395 | } |
396 | ||
397 | if (changeResolution === true) { | |
398 | this.updateResolution(file.resolution.id, changeResolutionDelay) | |
399 | ||
400 | // Wait some seconds in observation of our new resolution | |
401 | this.isAutoResolutionObservation = true | |
7ee4a4af C |
402 | |
403 | this.qualityObservationTimer = setTimeout(() => { | |
404 | this.isAutoResolutionObservation = false | |
405 | }, this.CONSTANTS.AUTO_QUALITY_OBSERVATION_TIME) | |
a8462c8e C |
406 | } |
407 | }, this.CONSTANTS.AUTO_QUALITY_SCHEDULER) | |
408 | } | |
409 | ||
410 | private isPlayerWaiting () { | |
7ee4a4af | 411 | return this.player && this.player.hasClass('vjs-waiting') |
a8462c8e C |
412 | } |
413 | ||
a22bfc3e | 414 | private runTorrentInfoScheduler () { |
3bcfff7f | 415 | this.torrentInfoInterval = setInterval(() => { |
bf5685f0 C |
416 | // Not initialized yet |
417 | if (this.torrent === undefined) return | |
418 | ||
419 | // Http fallback | |
420 | if (this.torrent === null) return this.trigger('torrentInfo', false) | |
421 | ||
a8462c8e C |
422 | // webtorrent.downloadSpeed because we need to take into account the potential old torrent too |
423 | if (webtorrent.downloadSpeed !== 0) this.downloadSpeeds.push(webtorrent.downloadSpeed) | |
424 | ||
bf5685f0 C |
425 | return this.trigger('torrentInfo', { |
426 | downloadSpeed: this.torrent.downloadSpeed, | |
427 | numPeers: this.torrent.numPeers, | |
428 | uploadSpeed: this.torrent.uploadSpeed | |
429 | }) | |
a8462c8e | 430 | }, this.CONSTANTS.INFO_SCHEDULER) |
a22bfc3e | 431 | } |
aa8b6df4 | 432 | |
8cac1b64 C |
433 | private runViewAdd () { |
434 | this.clearVideoViewInterval() | |
435 | ||
436 | // After 30 seconds (or 3/4 of the video), add a view to the video | |
437 | let minSecondsToView = 30 | |
438 | ||
3bcfff7f | 439 | if (this.videoDuration < minSecondsToView) minSecondsToView = (this.videoDuration * 3) / 4 |
8cac1b64 C |
440 | |
441 | let secondsViewed = 0 | |
442 | this.videoViewInterval = setInterval(() => { | |
443 | if (this.player && !this.player.paused()) { | |
444 | secondsViewed += 1 | |
445 | ||
446 | if (secondsViewed > minSecondsToView) { | |
447 | this.clearVideoViewInterval() | |
448 | ||
449 | this.addViewToVideo().catch(err => console.error(err)) | |
450 | } | |
451 | } | |
452 | }, 1000) | |
453 | } | |
454 | ||
455 | private clearVideoViewInterval () { | |
456 | if (this.videoViewInterval !== undefined) { | |
457 | clearInterval(this.videoViewInterval) | |
458 | this.videoViewInterval = undefined | |
459 | } | |
460 | } | |
461 | ||
462 | private addViewToVideo () { | |
463 | return fetch(this.videoViewUrl, { method: 'POST' }) | |
464 | } | |
465 | ||
e7eb5b39 | 466 | private fallbackToHttp (done?: Function, play = true) { |
bf5685f0 C |
467 | this.flushVideoFile(this.currentVideoFile, true) |
468 | this.torrent = null | |
469 | ||
470 | // Enable error display now this is our last fallback | |
471 | this.player.one('error', () => this.enableErrorDisplay()) | |
472 | ||
473 | const httpUrl = this.currentVideoFile.fileUrl | |
474 | this.player.src = this.savePlayerSrcFunction | |
475 | this.player.src(httpUrl) | |
e7eb5b39 | 476 | if (play) this.tryToPlay() |
c6352f2c | 477 | |
e7eb5b39 | 478 | if (done) return done() |
bf5685f0 C |
479 | } |
480 | ||
a22bfc3e C |
481 | private handleError (err: Error | string) { |
482 | return this.player.trigger('customError', { err }) | |
aa8b6df4 | 483 | } |
bf5685f0 C |
484 | |
485 | private enableErrorDisplay () { | |
486 | this.player.addClass('vjs-error-display-enabled') | |
487 | } | |
488 | ||
489 | private disableErrorDisplay () { | |
490 | this.player.removeClass('vjs-error-display-enabled') | |
491 | } | |
e993ecb3 | 492 | |
e7eb5b39 C |
493 | private isIOS () { |
494 | return !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform) | |
495 | } | |
496 | ||
c6352f2c C |
497 | private alterInactivity () { |
498 | let saveInactivityTimeout: number | |
499 | ||
500 | const disableInactivity = () => { | |
501 | saveInactivityTimeout = this.player.options_.inactivityTimeout | |
502 | this.player.options_.inactivityTimeout = 0 | |
503 | } | |
504 | const enableInactivity = () => { | |
b891f9bc | 505 | this.player.options_.inactivityTimeout = saveInactivityTimeout |
c6352f2c C |
506 | } |
507 | ||
508 | const settingsDialog = this.player.children_.find(c => c.name_ === 'SettingsDialog') | |
509 | ||
510 | this.player.controlBar.on('mouseenter', () => disableInactivity()) | |
511 | settingsDialog.on('mouseenter', () => disableInactivity()) | |
512 | this.player.controlBar.on('mouseleave', () => enableInactivity()) | |
513 | settingsDialog.on('mouseleave', () => enableInactivity()) | |
514 | } | |
515 | ||
e993ecb3 C |
516 | // Thanks: https://github.com/videojs/video.js/issues/4460#issuecomment-312861657 |
517 | private initSmoothProgressBar () { | |
518 | const SeekBar = videojsUntyped.getComponent('SeekBar') | |
519 | SeekBar.prototype.getPercent = function getPercent () { | |
520 | // Allows for smooth scrubbing, when player can't keep up. | |
521 | // const time = (this.player_.scrubbing()) ? | |
522 | // this.player_.getCache().currentTime : | |
523 | // this.player_.currentTime() | |
524 | const time = this.player_.currentTime() | |
525 | const percent = time / this.player_.duration() | |
526 | return percent >= 1 ? 1 : percent | |
527 | } | |
528 | SeekBar.prototype.handleMouseMove = function handleMouseMove (event) { | |
529 | let newTime = this.calculateDistance(event) * this.player_.duration() | |
530 | if (newTime === this.player_.duration()) { | |
531 | newTime = newTime - 0.1 | |
532 | } | |
533 | this.player_.currentTime(newTime) | |
534 | this.update() | |
535 | } | |
536 | } | |
aa8b6df4 | 537 | } |
c6352f2c | 538 | |
a22bfc3e | 539 | videojsUntyped.registerPlugin('peertube', PeerTubePlugin) |
c6352f2c | 540 | export { PeerTubePlugin } |