diff options
Diffstat (limited to 'client/src/assets/player/peertube-videojs-plugin.ts')
-rw-r--r-- | client/src/assets/player/peertube-videojs-plugin.ts | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/client/src/assets/player/peertube-videojs-plugin.ts b/client/src/assets/player/peertube-videojs-plugin.ts index adc376e94..2330f476f 100644 --- a/client/src/assets/player/peertube-videojs-plugin.ts +++ b/client/src/assets/player/peertube-videojs-plugin.ts | |||
@@ -3,7 +3,7 @@ import * as WebTorrent from 'webtorrent' | |||
3 | import { VideoFile } from '../../../../shared/models/videos/video.model' | 3 | import { VideoFile } from '../../../../shared/models/videos/video.model' |
4 | import { renderVideo } from './video-renderer' | 4 | import { renderVideo } from './video-renderer' |
5 | import './settings-menu-button' | 5 | import './settings-menu-button' |
6 | import { PeertubePluginOptions, VideoJSCaption, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' | 6 | import { PeertubePluginOptions, UserWatching, VideoJSCaption, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' |
7 | import { isMobile, timeToInt, videoFileMaxByResolution, videoFileMinByResolution } from './utils' | 7 | import { isMobile, timeToInt, videoFileMaxByResolution, videoFileMinByResolution } from './utils' |
8 | import * as CacheChunkStore from 'cache-chunk-store' | 8 | import * as CacheChunkStore from 'cache-chunk-store' |
9 | import { PeertubeChunkStore } from './peertube-chunk-store' | 9 | import { PeertubeChunkStore } from './peertube-chunk-store' |
@@ -32,7 +32,8 @@ class PeerTubePlugin extends Plugin { | |||
32 | AUTO_QUALITY_THRESHOLD_PERCENT: 30, // Bandwidth should be 30% more important than a resolution bitrate to change to it | 32 | AUTO_QUALITY_THRESHOLD_PERCENT: 30, // Bandwidth should be 30% more important than a resolution bitrate to change to it |
33 | AUTO_QUALITY_OBSERVATION_TIME: 10000, // Wait 10 seconds after having change the resolution before another check | 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 | 34 | AUTO_QUALITY_HIGHER_RESOLUTION_DELAY: 5000, // Buffering higher resolution during 5 seconds |
35 | BANDWIDTH_AVERAGE_NUMBER_OF_VALUES: 5 // Last 5 seconds to build average bandwidth | 35 | BANDWIDTH_AVERAGE_NUMBER_OF_VALUES: 5, // Last 5 seconds to build average bandwidth |
36 | USER_WATCHING_VIDEO_INTERVAL: 5000 // Every 5 seconds, notify the user is watching the video | ||
36 | } | 37 | } |
37 | 38 | ||
38 | private readonly webtorrent = new WebTorrent({ | 39 | private readonly webtorrent = new WebTorrent({ |
@@ -67,6 +68,7 @@ class PeerTubePlugin extends Plugin { | |||
67 | private videoViewInterval | 68 | private videoViewInterval |
68 | private torrentInfoInterval | 69 | private torrentInfoInterval |
69 | private autoQualityInterval | 70 | private autoQualityInterval |
71 | private userWatchingVideoInterval | ||
70 | private addTorrentDelay | 72 | private addTorrentDelay |
71 | private qualityObservationTimer | 73 | private qualityObservationTimer |
72 | private runAutoQualitySchedulerTimer | 74 | private runAutoQualitySchedulerTimer |
@@ -100,6 +102,8 @@ class PeerTubePlugin extends Plugin { | |||
100 | this.runTorrentInfoScheduler() | 102 | this.runTorrentInfoScheduler() |
101 | this.runViewAdd() | 103 | this.runViewAdd() |
102 | 104 | ||
105 | if (options.userWatching) this.runUserWatchVideo(options.userWatching) | ||
106 | |||
103 | this.player.one('play', () => { | 107 | this.player.one('play', () => { |
104 | // Don't run immediately scheduler, wait some seconds the TCP connections are made | 108 | // Don't run immediately scheduler, wait some seconds the TCP connections are made |
105 | this.runAutoQualitySchedulerTimer = setTimeout(() => this.runAutoQualityScheduler(), this.CONSTANTS.AUTO_QUALITY_SCHEDULER) | 109 | this.runAutoQualitySchedulerTimer = setTimeout(() => this.runAutoQualityScheduler(), this.CONSTANTS.AUTO_QUALITY_SCHEDULER) |
@@ -121,6 +125,8 @@ class PeerTubePlugin extends Plugin { | |||
121 | clearInterval(this.torrentInfoInterval) | 125 | clearInterval(this.torrentInfoInterval) |
122 | clearInterval(this.autoQualityInterval) | 126 | clearInterval(this.autoQualityInterval) |
123 | 127 | ||
128 | if (this.userWatchingVideoInterval) clearInterval(this.userWatchingVideoInterval) | ||
129 | |||
124 | // Don't need to destroy renderer, video player will be destroyed | 130 | // Don't need to destroy renderer, video player will be destroyed |
125 | this.flushVideoFile(this.currentVideoFile, false) | 131 | this.flushVideoFile(this.currentVideoFile, false) |
126 | 132 | ||
@@ -524,6 +530,21 @@ class PeerTubePlugin extends Plugin { | |||
524 | }, 1000) | 530 | }, 1000) |
525 | } | 531 | } |
526 | 532 | ||
533 | private runUserWatchVideo (options: UserWatching) { | ||
534 | let lastCurrentTime = 0 | ||
535 | |||
536 | this.userWatchingVideoInterval = setInterval(() => { | ||
537 | const currentTime = Math.floor(this.player.currentTime()) | ||
538 | |||
539 | if (currentTime - lastCurrentTime >= 1) { | ||
540 | lastCurrentTime = currentTime | ||
541 | |||
542 | this.notifyUserIsWatching(currentTime, options.url, options.authorizationHeader) | ||
543 | .catch(err => console.error('Cannot notify user is watching.', err)) | ||
544 | } | ||
545 | }, this.CONSTANTS.USER_WATCHING_VIDEO_INTERVAL) | ||
546 | } | ||
547 | |||
527 | private clearVideoViewInterval () { | 548 | private clearVideoViewInterval () { |
528 | if (this.videoViewInterval !== undefined) { | 549 | if (this.videoViewInterval !== undefined) { |
529 | clearInterval(this.videoViewInterval) | 550 | clearInterval(this.videoViewInterval) |
@@ -537,6 +558,15 @@ class PeerTubePlugin extends Plugin { | |||
537 | return fetch(this.videoViewUrl, { method: 'POST' }) | 558 | return fetch(this.videoViewUrl, { method: 'POST' }) |
538 | } | 559 | } |
539 | 560 | ||
561 | private notifyUserIsWatching (currentTime: number, url: string, authorizationHeader: string) { | ||
562 | const body = new URLSearchParams() | ||
563 | body.append('currentTime', currentTime.toString()) | ||
564 | |||
565 | const headers = new Headers({ 'Authorization': authorizationHeader }) | ||
566 | |||
567 | return fetch(url, { method: 'PUT', body, headers }) | ||
568 | } | ||
569 | |||
540 | private fallbackToHttp (done?: Function, play = true) { | 570 | private fallbackToHttp (done?: Function, play = true) { |
541 | this.disableAutoResolution(true) | 571 | this.disableAutoResolution(true) |
542 | 572 | ||