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