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