1 import videojs from 'video.js'
2 import './videojs-components/settings-menu-button'
8 } from './peertube-videojs-typings'
9 import { isMobile, timeToInt } from './utils'
11 getStoredLastSubtitle,
17 } from './peertube-player-local-storage'
19 const Plugin = videojs.getPlugin('plugin')
21 class PeerTubePlugin extends Plugin {
22 private readonly videoViewUrl: string
23 private readonly videoDuration: number
24 private readonly CONSTANTS = {
25 USER_WATCHING_VIDEO_INTERVAL: 5000 // Every 5 seconds, notify the user is watching the video
28 private videoCaptions: VideoJSCaption[]
29 private defaultSubtitle: string
31 private videoViewInterval: any
32 private userWatchingVideoInterval: any
33 private lastResolutionChange: ResolutionUpdateData
35 private menuOpened = false
36 private mouseInControlBar = false
37 private readonly savedInactivityTimeout: number
39 constructor (player: videojs.Player, options?: PeerTubePluginOptions) {
42 this.videoViewUrl = options.videoViewUrl
43 this.videoDuration = options.videoDuration
44 this.videoCaptions = options.videoCaptions
46 this.savedInactivityTimeout = player.options_.inactivityTimeout
48 if (options.autoplay === true) this.player.addClass('vjs-has-autoplay')
50 this.player.on('autoplay-failure', () => {
51 this.player.removeClass('vjs-has-autoplay')
54 this.player.ready(() => {
55 const playerOptions = this.player.options_
57 if (options.mode === 'webtorrent') {
58 this.player.webtorrent().on('resolutionChange', (_: any, d: any) => this.handleResolutionChange(d))
59 this.player.webtorrent().on('autoResolutionChange', (_: any, d: any) => this.trigger('autoResolutionChange', d))
62 if (options.mode === 'p2p-media-loader') {
63 this.player.p2pMediaLoader().on('resolutionChange', (_: any, d: any) => this.handleResolutionChange(d))
66 this.player.tech(true).on('loadedqualitydata', () => {
68 // Replay a resolution change, now we loaded all quality data
69 if (this.lastResolutionChange) this.handleResolutionChange(this.lastResolutionChange)
73 const volume = getStoredVolume()
74 if (volume !== undefined) this.player.volume(volume)
76 const muted = playerOptions.muted !== undefined ? playerOptions.muted : getStoredMute()
77 if (muted !== undefined) this.player.muted(muted)
79 this.defaultSubtitle = options.subtitle || getStoredLastSubtitle()
81 this.player.on('volumechange', () => {
82 saveVolumeInStore(this.player.volume())
83 saveMuteInStore(this.player.muted())
86 if (options.stopTime) {
87 const stopTime = timeToInt(options.stopTime)
90 this.player.on('timeupdate', function onTimeUpdate () {
91 if (self.player.currentTime() > stopTime) {
93 self.player.trigger('stopped')
95 self.player.off('timeupdate', onTimeUpdate)
100 this.player.textTracks().on('change', () => {
101 const showing = this.player.textTracks().tracks_.find(t => {
102 return t.kind === 'captions' && t.mode === 'showing'
106 saveLastSubtitle('off')
110 saveLastSubtitle(showing.language)
113 this.player.on('sourcechange', () => this.initCaptions())
115 this.player.duration(options.videoDuration)
117 this.initializePlayer()
120 if (options.userWatching) this.runUserWatchVideo(options.userWatching)
125 if (this.videoViewInterval) clearInterval(this.videoViewInterval)
126 if (this.userWatchingVideoInterval) clearInterval(this.userWatchingVideoInterval)
130 this.menuOpened = false
131 this.alterInactivity()
135 this.menuOpened = true
136 this.alterInactivity()
139 private initializePlayer () {
140 if (isMobile()) this.player.addClass('vjs-is-mobile')
142 this.initSmoothProgressBar()
146 this.listenControlBarMouse()
149 private runViewAdd () {
150 this.clearVideoViewInterval()
152 // After 30 seconds (or 3/4 of the video), add a view to the video
153 let minSecondsToView = 30
155 if (this.videoDuration < minSecondsToView) minSecondsToView = (this.videoDuration * 3) / 4
157 let secondsViewed = 0
158 this.videoViewInterval = setInterval(() => {
159 if (this.player && !this.player.paused()) {
162 if (secondsViewed > minSecondsToView) {
163 this.clearVideoViewInterval()
165 this.addViewToVideo().catch(err => console.error(err))
171 private runUserWatchVideo (options: UserWatching) {
172 let lastCurrentTime = 0
174 this.userWatchingVideoInterval = setInterval(() => {
175 const currentTime = Math.floor(this.player.currentTime())
177 if (currentTime - lastCurrentTime >= 1) {
178 lastCurrentTime = currentTime
180 this.notifyUserIsWatching(currentTime, options.url, options.authorizationHeader)
181 .catch(err => console.error('Cannot notify user is watching.', err))
183 }, this.CONSTANTS.USER_WATCHING_VIDEO_INTERVAL)
186 private clearVideoViewInterval () {
187 if (this.videoViewInterval !== undefined) {
188 clearInterval(this.videoViewInterval)
189 this.videoViewInterval = undefined
193 private addViewToVideo () {
194 if (!this.videoViewUrl) return Promise.resolve(undefined)
196 return fetch(this.videoViewUrl, { method: 'POST' })
199 private notifyUserIsWatching (currentTime: number, url: string, authorizationHeader: string) {
200 const body = new URLSearchParams()
201 body.append('currentTime', currentTime.toString())
203 const headers = new Headers({ 'Authorization': authorizationHeader })
205 return fetch(url, { method: 'PUT', body, headers })
208 private handleResolutionChange (data: ResolutionUpdateData) {
209 this.lastResolutionChange = data
211 const qualityLevels = this.player.qualityLevels()
213 for (let i = 0; i < qualityLevels.length; i++) {
214 if (qualityLevels[i].height === data.resolutionId) {
215 data.id = qualityLevels[i].id
220 this.trigger('resolutionChange', data)
223 private listenControlBarMouse () {
224 this.player.controlBar.on('mouseenter', () => {
225 this.mouseInControlBar = true
226 this.alterInactivity()
229 this.player.controlBar.on('mouseleave', () => {
230 this.mouseInControlBar = false
231 this.alterInactivity()
235 private alterInactivity () {
236 if (this.menuOpened || this.mouseInControlBar) {
237 this.player.options_.inactivityTimeout = this.savedInactivityTimeout
241 this.player.options_.inactivityTimeout = 1
244 private initCaptions () {
245 for (const caption of this.videoCaptions) {
246 this.player.addRemoteTextTrack({
248 label: caption.label,
249 language: caption.language,
250 id: caption.language,
252 default: this.defaultSubtitle === caption.language
256 this.player.trigger('captionsChanged')
259 // Thanks: https://github.com/videojs/video.js/issues/4460#issuecomment-312861657
260 private initSmoothProgressBar () {
261 const SeekBar = videojs.getComponent('SeekBar') as any
262 SeekBar.prototype.getPercent = function getPercent () {
263 // Allows for smooth scrubbing, when player can't keep up.
264 // const time = (this.player_.scrubbing()) ?
265 // this.player_.getCache().currentTime :
266 // this.player_.currentTime()
267 const time = this.player_.currentTime()
268 const percent = time / this.player_.duration()
269 return percent >= 1 ? 1 : percent
271 SeekBar.prototype.handleMouseMove = function handleMouseMove (event: any) {
272 let newTime = this.calculateDistance(event) * this.player_.duration()
273 if (newTime === this.player_.duration()) {
274 newTime = newTime - 0.1
276 this.player_.currentTime(newTime)
282 videojs.registerPlugin('peertube', PeerTubePlugin)
283 export { PeerTubePlugin }