]>
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' |
7b3a99d5 C |
7 | import { isMobile, videoFileMaxByResolution, videoFileMinByResolution } from './utils' |
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 | |
f37bad63 | 79 | this.startTime = 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.') | |
3baf9be2 C |
267 | const options = { forcePlay: true } |
268 | return this.addTorrent(this.torrent['xs'], previousVideoFile, options, done) | |
a216c623 C |
269 | } |
270 | ||
332e7032 | 271 | return console.warn(err) |
a96aed15 | 272 | }) |
aa8b6df4 C |
273 | } |
274 | ||
a8462c8e | 275 | updateResolution (resolutionId: number, delay = 0) { |
aa8b6df4 | 276 | // Remember player state |
a22bfc3e C |
277 | const currentTime = this.player.currentTime() |
278 | const isPaused = this.player.paused() | |
aa8b6df4 | 279 | |
531ab5b6 C |
280 | // Remove poster to have black background |
281 | this.playerElement.poster = '' | |
282 | ||
aa8b6df4 | 283 | // Hide bigPlayButton |
8fa5653a | 284 | if (!isPaused) { |
a22bfc3e | 285 | this.player.bigPlayButton.hide() |
aa8b6df4 C |
286 | } |
287 | ||
09700934 | 288 | const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId) |
3baf9be2 C |
289 | const options = { |
290 | forcePlay: false, | |
291 | delay, | |
91d95589 | 292 | seek: currentTime + (delay / 1000) |
3baf9be2 C |
293 | } |
294 | this.updateVideoFile(newVideoFile, options) | |
aa8b6df4 C |
295 | } |
296 | ||
a22bfc3e | 297 | flushVideoFile (videoFile: VideoFile, destroyRenderer = true) { |
b7f1747d | 298 | if (videoFile !== undefined && this.webtorrent.get(videoFile.magnetUri)) { |
bf5685f0 C |
299 | if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy() |
300 | ||
b7f1747d | 301 | this.webtorrent.remove(videoFile.magnetUri) |
a22bfc3e | 302 | console.log('Removed ' + videoFile.magnetUri) |
aa8b6df4 C |
303 | } |
304 | } | |
305 | ||
a8462c8e C |
306 | isAutoResolutionOn () { |
307 | return this.autoResolution | |
308 | } | |
309 | ||
310 | enableAutoResolution () { | |
311 | this.autoResolution = true | |
312 | this.trigger('autoResolutionUpdate') | |
313 | } | |
314 | ||
c4082b8b C |
315 | disableAutoResolution (forbid = false) { |
316 | if (forbid === true) this.forbidAutoResolution = true | |
317 | ||
a8462c8e C |
318 | this.autoResolution = false |
319 | this.trigger('autoResolutionUpdate') | |
320 | } | |
321 | ||
c4082b8b C |
322 | isAutoResolutionForbidden () { |
323 | return this.forbidAutoResolution === true | |
324 | } | |
325 | ||
960a11e8 C |
326 | getCurrentVideoFile () { |
327 | return this.currentVideoFile | |
328 | } | |
329 | ||
77728efa C |
330 | getTorrent () { |
331 | return this.torrent | |
332 | } | |
333 | ||
80109b2d C |
334 | private tryToPlay (done?: Function) { |
335 | if (!done) done = function () { /* empty */ } | |
1fad099d | 336 | |
80109b2d C |
337 | const playPromise = this.player.play() |
338 | if (playPromise !== undefined) { | |
339 | return playPromise.then(done) | |
340 | .catch(err => { | |
70b40c2e C |
341 | if (err.message.indexOf('The play() request was interrupted by a call to pause()') !== -1) { |
342 | return | |
343 | } | |
344 | ||
80109b2d C |
345 | console.error(err) |
346 | this.player.pause() | |
347 | this.player.posterImage.show() | |
348 | this.player.removeClass('vjs-has-autoplay') | |
91d95589 | 349 | this.player.removeClass('vjs-has-big-play-button-clicked') |
80109b2d C |
350 | |
351 | return done() | |
352 | }) | |
353 | } | |
354 | ||
355 | return done() | |
356 | } | |
357 | ||
f37bad63 C |
358 | private seek (time: number) { |
359 | this.player.currentTime(time) | |
360 | this.player.handleTechSeeked_() | |
361 | } | |
362 | ||
a8462c8e C |
363 | private getAppropriateFile (averageDownloadSpeed?: number): VideoFile { |
364 | if (this.videoFiles === undefined || this.videoFiles.length === 0) return undefined | |
365 | if (this.videoFiles.length === 1) return this.videoFiles[0] | |
a8462c8e | 366 | |
3c40590d C |
367 | // Don't change the torrent is the play was ended |
368 | if (this.torrent && this.torrent.progress === 1 && this.player.ended()) return this.currentVideoFile | |
369 | ||
370 | if (!averageDownloadSpeed) averageDownloadSpeed = this.getAndSaveActualDownloadSpeed() | |
a8462c8e | 371 | |
0b755f3b C |
372 | // Limit resolution according to player height |
373 | const playerHeight = this.playerElement.offsetHeight as number | |
374 | ||
375 | // We take the first resolution just above the player height | |
376 | // Example: player height is 530px, we want the 720p file instead of 480p | |
377 | let maxResolution = this.videoFiles[0].resolution.id | |
378 | for (let i = this.videoFiles.length - 1; i >= 0; i--) { | |
379 | const resolutionId = this.videoFiles[i].resolution.id | |
380 | if (resolutionId >= playerHeight) { | |
381 | maxResolution = resolutionId | |
382 | break | |
a8462c8e | 383 | } |
0b755f3b | 384 | } |
a8462c8e | 385 | |
0b755f3b C |
386 | // Filter videos we can play according to our screen resolution and bandwidth |
387 | const filteredFiles = this.videoFiles | |
388 | .filter(f => f.resolution.id <= maxResolution) | |
389 | .filter(f => { | |
390 | const fileBitrate = (f.size / this.videoDuration) | |
391 | let threshold = fileBitrate | |
392 | ||
393 | // If this is for a higher resolution or an initial load: add a margin | |
394 | if (!this.currentVideoFile || f.resolution.id > this.currentVideoFile.resolution.id) { | |
395 | threshold += ((fileBitrate * this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT) / 100) | |
396 | } | |
397 | ||
398 | return averageDownloadSpeed > threshold | |
399 | }) | |
a8462c8e C |
400 | |
401 | // If the download speed is too bad, return the lowest resolution we have | |
6cca7360 | 402 | if (filteredFiles.length === 0) return videoFileMinByResolution(this.videoFiles) |
a8462c8e | 403 | |
6cca7360 | 404 | return videoFileMaxByResolution(filteredFiles) |
a8462c8e C |
405 | } |
406 | ||
3c40590d | 407 | private getAndSaveActualDownloadSpeed () { |
a8462c8e C |
408 | const start = Math.max(this.downloadSpeeds.length - this.CONSTANTS.BANDWIDTH_AVERAGE_NUMBER_OF_VALUES, 0) |
409 | const lastDownloadSpeeds = this.downloadSpeeds.slice(start, this.downloadSpeeds.length) | |
410 | if (lastDownloadSpeeds.length === 0) return -1 | |
411 | ||
412 | const sum = lastDownloadSpeeds.reduce((a, b) => a + b) | |
413 | const averageBandwidth = Math.round(sum / lastDownloadSpeeds.length) | |
414 | ||
415 | // Save the average bandwidth for future use | |
416 | saveAverageBandwidth(averageBandwidth) | |
ed9f9f5f | 417 | |
a8462c8e | 418 | return averageBandwidth |
ed9f9f5f C |
419 | } |
420 | ||
0dcf9a14 | 421 | private initializePlayer () { |
2ce2fd7f C |
422 | if (isMobile()) this.player.addClass('vjs-is-mobile') |
423 | ||
e993ecb3 C |
424 | this.initSmoothProgressBar() |
425 | ||
16f7022b C |
426 | this.initCaptions() |
427 | ||
c6352f2c C |
428 | this.alterInactivity() |
429 | ||
481d3596 | 430 | if (this.autoplay === true) { |
33d78552 | 431 | this.player.posterImage.hide() |
e6f62797 | 432 | |
3baf9be2 | 433 | this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime }) |
aa8b6df4 | 434 | } else { |
e7eb5b39 C |
435 | // Don't try on iOS that does not support MediaSource |
436 | if (this.isIOS()) { | |
8eb8bc20 | 437 | this.currentVideoFile = this.pickAverageVideoFile() |
e7eb5b39 C |
438 | return this.fallbackToHttp(undefined, false) |
439 | } | |
440 | ||
b891f9bc | 441 | // Proxy first play |
c6352f2c C |
442 | const oldPlay = this.player.play.bind(this.player) |
443 | this.player.play = () => { | |
864e782b | 444 | this.player.addClass('vjs-has-big-play-button-clicked') |
c6352f2c | 445 | this.player.play = oldPlay |
864e782b | 446 | |
3baf9be2 | 447 | this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime }) |
c6352f2c | 448 | } |
aa8b6df4 | 449 | } |
a22bfc3e | 450 | } |
aa8b6df4 | 451 | |
a8462c8e C |
452 | private runAutoQualityScheduler () { |
453 | this.autoQualityInterval = setInterval(() => { | |
7ee4a4af | 454 | |
877b0528 C |
455 | // Not initialized or in HTTP fallback |
456 | if (this.torrent === undefined || this.torrent === null) return | |
a8462c8e C |
457 | if (this.isAutoResolutionOn() === false) return |
458 | if (this.isAutoResolutionObservation === true) return | |
459 | ||
460 | const file = this.getAppropriateFile() | |
461 | let changeResolution = false | |
462 | let changeResolutionDelay = 0 | |
463 | ||
464 | // Lower resolution | |
465 | if (this.isPlayerWaiting() && file.resolution.id < this.currentVideoFile.resolution.id) { | |
466 | console.log('Downgrading automatically the resolution to: %s', file.resolution.label) | |
467 | changeResolution = true | |
7ee4a4af | 468 | } else if (file.resolution.id > this.currentVideoFile.resolution.id) { // Higher resolution |
a8462c8e C |
469 | console.log('Upgrading automatically the resolution to: %s', file.resolution.label) |
470 | changeResolution = true | |
7ee4a4af | 471 | changeResolutionDelay = this.CONSTANTS.AUTO_QUALITY_HIGHER_RESOLUTION_DELAY |
a8462c8e C |
472 | } |
473 | ||
474 | if (changeResolution === true) { | |
475 | this.updateResolution(file.resolution.id, changeResolutionDelay) | |
476 | ||
477 | // Wait some seconds in observation of our new resolution | |
478 | this.isAutoResolutionObservation = true | |
7ee4a4af C |
479 | |
480 | this.qualityObservationTimer = setTimeout(() => { | |
481 | this.isAutoResolutionObservation = false | |
482 | }, this.CONSTANTS.AUTO_QUALITY_OBSERVATION_TIME) | |
a8462c8e C |
483 | } |
484 | }, this.CONSTANTS.AUTO_QUALITY_SCHEDULER) | |
485 | } | |
486 | ||
487 | private isPlayerWaiting () { | |
7ee4a4af | 488 | return this.player && this.player.hasClass('vjs-waiting') |
a8462c8e C |
489 | } |
490 | ||
a22bfc3e | 491 | private runTorrentInfoScheduler () { |
3bcfff7f | 492 | this.torrentInfoInterval = setInterval(() => { |
bf5685f0 C |
493 | // Not initialized yet |
494 | if (this.torrent === undefined) return | |
495 | ||
496 | // Http fallback | |
497 | if (this.torrent === null) return this.trigger('torrentInfo', false) | |
498 | ||
b7f1747d C |
499 | // this.webtorrent.downloadSpeed because we need to take into account the potential old torrent too |
500 | if (this.webtorrent.downloadSpeed !== 0) this.downloadSpeeds.push(this.webtorrent.downloadSpeed) | |
a8462c8e | 501 | |
bf5685f0 C |
502 | return this.trigger('torrentInfo', { |
503 | downloadSpeed: this.torrent.downloadSpeed, | |
504 | numPeers: this.torrent.numPeers, | |
1a49822c C |
505 | uploadSpeed: this.torrent.uploadSpeed, |
506 | downloaded: this.torrent.downloaded, | |
507 | uploaded: this.torrent.uploaded | |
bf5685f0 | 508 | }) |
a8462c8e | 509 | }, this.CONSTANTS.INFO_SCHEDULER) |
a22bfc3e | 510 | } |
aa8b6df4 | 511 | |
8cac1b64 C |
512 | private runViewAdd () { |
513 | this.clearVideoViewInterval() | |
514 | ||
515 | // After 30 seconds (or 3/4 of the video), add a view to the video | |
516 | let minSecondsToView = 30 | |
517 | ||
3bcfff7f | 518 | if (this.videoDuration < minSecondsToView) minSecondsToView = (this.videoDuration * 3) / 4 |
8cac1b64 C |
519 | |
520 | let secondsViewed = 0 | |
521 | this.videoViewInterval = setInterval(() => { | |
522 | if (this.player && !this.player.paused()) { | |
523 | secondsViewed += 1 | |
524 | ||
525 | if (secondsViewed > minSecondsToView) { | |
526 | this.clearVideoViewInterval() | |
527 | ||
528 | this.addViewToVideo().catch(err => console.error(err)) | |
529 | } | |
530 | } | |
531 | }, 1000) | |
532 | } | |
533 | ||
534 | private clearVideoViewInterval () { | |
535 | if (this.videoViewInterval !== undefined) { | |
536 | clearInterval(this.videoViewInterval) | |
537 | this.videoViewInterval = undefined | |
538 | } | |
539 | } | |
540 | ||
541 | private addViewToVideo () { | |
f5a2dc48 C |
542 | if (!this.videoViewUrl) return Promise.resolve(undefined) |
543 | ||
8cac1b64 C |
544 | return fetch(this.videoViewUrl, { method: 'POST' }) |
545 | } | |
546 | ||
e7eb5b39 | 547 | private fallbackToHttp (done?: Function, play = true) { |
c4082b8b C |
548 | this.disableAutoResolution(true) |
549 | ||
bf5685f0 C |
550 | this.flushVideoFile(this.currentVideoFile, true) |
551 | this.torrent = null | |
552 | ||
553 | // Enable error display now this is our last fallback | |
554 | this.player.one('error', () => this.enableErrorDisplay()) | |
555 | ||
556 | const httpUrl = this.currentVideoFile.fileUrl | |
557 | this.player.src = this.savePlayerSrcFunction | |
558 | this.player.src(httpUrl) | |
e7eb5b39 | 559 | if (play) this.tryToPlay() |
c6352f2c | 560 | |
e7eb5b39 | 561 | if (done) return done() |
bf5685f0 C |
562 | } |
563 | ||
a22bfc3e C |
564 | private handleError (err: Error | string) { |
565 | return this.player.trigger('customError', { err }) | |
aa8b6df4 | 566 | } |
bf5685f0 C |
567 | |
568 | private enableErrorDisplay () { | |
569 | this.player.addClass('vjs-error-display-enabled') | |
570 | } | |
571 | ||
572 | private disableErrorDisplay () { | |
573 | this.player.removeClass('vjs-error-display-enabled') | |
574 | } | |
e993ecb3 | 575 | |
e7eb5b39 C |
576 | private isIOS () { |
577 | return !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform) | |
578 | } | |
579 | ||
c6352f2c C |
580 | private alterInactivity () { |
581 | let saveInactivityTimeout: number | |
582 | ||
583 | const disableInactivity = () => { | |
584 | saveInactivityTimeout = this.player.options_.inactivityTimeout | |
585 | this.player.options_.inactivityTimeout = 0 | |
586 | } | |
587 | const enableInactivity = () => { | |
16f7022b | 588 | // this.player.options_.inactivityTimeout = saveInactivityTimeout |
c6352f2c C |
589 | } |
590 | ||
591 | const settingsDialog = this.player.children_.find(c => c.name_ === 'SettingsDialog') | |
592 | ||
593 | this.player.controlBar.on('mouseenter', () => disableInactivity()) | |
594 | settingsDialog.on('mouseenter', () => disableInactivity()) | |
595 | this.player.controlBar.on('mouseleave', () => enableInactivity()) | |
332e7032 | 596 | settingsDialog.on('mouseleave', () => enableInactivity()) |
c6352f2c C |
597 | } |
598 | ||
8eb8bc20 C |
599 | private pickAverageVideoFile () { |
600 | if (this.videoFiles.length === 1) return this.videoFiles[0] | |
601 | ||
602 | return this.videoFiles[Math.floor(this.videoFiles.length / 2)] | |
603 | } | |
604 | ||
054a103b C |
605 | private destroyFakeRenderer () { |
606 | if (this.fakeRenderer) { | |
607 | if (this.fakeRenderer.destroy) { | |
608 | try { | |
609 | this.fakeRenderer.destroy() | |
610 | } catch (err) { | |
611 | console.log('Cannot destroy correctly fake renderer.', err) | |
612 | } | |
613 | } | |
614 | this.fakeRenderer = undefined | |
615 | } | |
616 | } | |
617 | ||
16f7022b C |
618 | private initCaptions () { |
619 | for (const caption of this.videoCaptions) { | |
620 | this.player.addRemoteTextTrack({ | |
621 | kind: 'captions', | |
622 | label: caption.label, | |
623 | language: caption.language, | |
624 | id: caption.language, | |
625 | src: caption.src | |
626 | }, false) | |
627 | } | |
628 | } | |
629 | ||
e993ecb3 C |
630 | // Thanks: https://github.com/videojs/video.js/issues/4460#issuecomment-312861657 |
631 | private initSmoothProgressBar () { | |
632 | const SeekBar = videojsUntyped.getComponent('SeekBar') | |
633 | SeekBar.prototype.getPercent = function getPercent () { | |
634 | // Allows for smooth scrubbing, when player can't keep up. | |
635 | // const time = (this.player_.scrubbing()) ? | |
636 | // this.player_.getCache().currentTime : | |
637 | // this.player_.currentTime() | |
638 | const time = this.player_.currentTime() | |
639 | const percent = time / this.player_.duration() | |
640 | return percent >= 1 ? 1 : percent | |
641 | } | |
642 | SeekBar.prototype.handleMouseMove = function handleMouseMove (event) { | |
643 | let newTime = this.calculateDistance(event) * this.player_.duration() | |
644 | if (newTime === this.player_.duration()) { | |
645 | newTime = newTime - 0.1 | |
646 | } | |
647 | this.player_.currentTime(newTime) | |
648 | this.update() | |
649 | } | |
650 | } | |
aa8b6df4 | 651 | } |
c6352f2c | 652 | |
b7f1747d | 653 | videojs.registerPlugin('peertube', PeerTubePlugin) |
c6352f2c | 654 | export { PeerTubePlugin } |