2021-07-26 15:04:37 +02:00
|
|
|
import videojs from 'video.js'
|
|
|
|
import { timeToInt } from '@shared/core-utils'
|
2019-01-23 15:36:45 +01:00
|
|
|
import {
|
|
|
|
getStoredLastSubtitle,
|
|
|
|
getStoredMute,
|
|
|
|
getStoredVolume,
|
|
|
|
saveLastSubtitle,
|
|
|
|
saveMuteInStore,
|
2021-03-31 11:26:32 +02:00
|
|
|
saveVideoWatchHistory,
|
2019-01-23 15:36:45 +01:00
|
|
|
saveVolumeInStore
|
|
|
|
} from './peertube-player-local-storage'
|
2021-09-06 16:08:59 +02:00
|
|
|
import { PeerTubePluginOptions, UserWatching, VideoJSCaption } from './peertube-videojs-typings'
|
2021-07-26 15:04:37 +02:00
|
|
|
import { isMobile } from './utils'
|
2021-12-09 10:33:32 +01:00
|
|
|
import { SettingsButton } from './videojs-components/settings-menu-button'
|
2019-01-23 15:36:45 +01:00
|
|
|
|
2020-01-28 17:29:50 +01:00
|
|
|
const Plugin = videojs.getPlugin('plugin')
|
|
|
|
|
2019-01-23 15:36:45 +01:00
|
|
|
class PeerTubePlugin extends Plugin {
|
|
|
|
private readonly videoViewUrl: string
|
|
|
|
private readonly videoDuration: number
|
|
|
|
private readonly CONSTANTS = {
|
|
|
|
USER_WATCHING_VIDEO_INTERVAL: 5000 // Every 5 seconds, notify the user is watching the video
|
|
|
|
}
|
|
|
|
|
|
|
|
private videoCaptions: VideoJSCaption[]
|
|
|
|
private defaultSubtitle: string
|
|
|
|
|
|
|
|
private videoViewInterval: any
|
|
|
|
private userWatchingVideoInterval: any
|
|
|
|
|
2020-12-07 15:58:57 +01:00
|
|
|
private isLive: boolean
|
|
|
|
|
2019-08-07 10:17:19 +02:00
|
|
|
private menuOpened = false
|
|
|
|
private mouseInControlBar = false
|
2021-12-09 10:33:32 +01:00
|
|
|
private mouseInSettings = false
|
|
|
|
private readonly initialInactivityTimeout: number
|
2019-08-07 10:17:19 +02:00
|
|
|
|
2020-04-17 11:20:12 +02:00
|
|
|
constructor (player: videojs.Player, options?: PeerTubePluginOptions) {
|
2020-01-28 17:29:50 +01:00
|
|
|
super(player)
|
2019-01-23 15:36:45 +01:00
|
|
|
|
|
|
|
this.videoViewUrl = options.videoViewUrl
|
|
|
|
this.videoDuration = options.videoDuration
|
|
|
|
this.videoCaptions = options.videoCaptions
|
2020-12-07 15:58:57 +01:00
|
|
|
this.isLive = options.isLive
|
2021-12-09 10:33:32 +01:00
|
|
|
this.initialInactivityTimeout = this.player.options_.inactivityTimeout
|
2019-08-07 10:17:19 +02:00
|
|
|
|
2020-05-11 17:48:25 +02:00
|
|
|
if (options.autoplay) this.player.addClass('vjs-has-autoplay')
|
2019-02-06 10:39:50 +01:00
|
|
|
|
|
|
|
this.player.on('autoplay-failure', () => {
|
|
|
|
this.player.removeClass('vjs-has-autoplay')
|
|
|
|
})
|
2019-01-23 15:36:45 +01:00
|
|
|
|
|
|
|
this.player.ready(() => {
|
|
|
|
const playerOptions = this.player.options_
|
|
|
|
|
|
|
|
const volume = getStoredVolume()
|
|
|
|
if (volume !== undefined) this.player.volume(volume)
|
|
|
|
|
|
|
|
const muted = playerOptions.muted !== undefined ? playerOptions.muted : getStoredMute()
|
|
|
|
if (muted !== undefined) this.player.muted(muted)
|
|
|
|
|
|
|
|
this.defaultSubtitle = options.subtitle || getStoredLastSubtitle()
|
|
|
|
|
|
|
|
this.player.on('volumechange', () => {
|
|
|
|
saveVolumeInStore(this.player.volume())
|
|
|
|
saveMuteInStore(this.player.muted())
|
|
|
|
})
|
|
|
|
|
2019-03-07 17:06:00 +01:00
|
|
|
if (options.stopTime) {
|
|
|
|
const stopTime = timeToInt(options.stopTime)
|
2019-03-13 14:18:58 +01:00
|
|
|
const self = this
|
2019-03-07 17:06:00 +01:00
|
|
|
|
2019-03-13 14:18:58 +01:00
|
|
|
this.player.on('timeupdate', function onTimeUpdate () {
|
|
|
|
if (self.player.currentTime() > stopTime) {
|
|
|
|
self.player.pause()
|
|
|
|
self.player.trigger('stopped')
|
|
|
|
|
|
|
|
self.player.off('timeupdate', onTimeUpdate)
|
|
|
|
}
|
2019-03-07 17:06:00 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-06 16:08:59 +02:00
|
|
|
this.player.textTracks().addEventListener('change', () => {
|
2020-01-28 17:29:50 +01:00
|
|
|
const showing = this.player.textTracks().tracks_.find(t => {
|
2019-01-23 15:36:45 +01:00
|
|
|
return t.kind === 'captions' && t.mode === 'showing'
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!showing) {
|
|
|
|
saveLastSubtitle('off')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
saveLastSubtitle(showing.language)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.player.on('sourcechange', () => this.initCaptions())
|
|
|
|
|
|
|
|
this.player.duration(options.videoDuration)
|
|
|
|
|
|
|
|
this.initializePlayer()
|
|
|
|
this.runViewAdd()
|
|
|
|
|
2021-03-31 11:26:32 +02:00
|
|
|
this.runUserWatchVideo(options.userWatching, options.videoUUID)
|
2019-01-23 15:36:45 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose () {
|
2019-03-07 17:06:00 +01:00
|
|
|
if (this.videoViewInterval) clearInterval(this.videoViewInterval)
|
2019-01-23 15:36:45 +01:00
|
|
|
if (this.userWatchingVideoInterval) clearInterval(this.userWatchingVideoInterval)
|
|
|
|
}
|
|
|
|
|
2021-12-09 10:33:32 +01:00
|
|
|
onMenuOpened () {
|
|
|
|
this.menuOpened = true
|
2019-08-07 10:17:19 +02:00
|
|
|
this.alterInactivity()
|
|
|
|
}
|
|
|
|
|
|
|
|
onMenuClosed () {
|
2021-12-09 10:33:32 +01:00
|
|
|
this.menuOpened = false
|
2019-08-07 10:17:19 +02:00
|
|
|
this.alterInactivity()
|
|
|
|
}
|
|
|
|
|
2019-01-23 15:36:45 +01:00
|
|
|
private initializePlayer () {
|
|
|
|
if (isMobile()) this.player.addClass('vjs-is-mobile')
|
|
|
|
|
|
|
|
this.initSmoothProgressBar()
|
|
|
|
|
|
|
|
this.initCaptions()
|
|
|
|
|
2019-08-07 10:17:19 +02:00
|
|
|
this.listenControlBarMouse()
|
2021-12-09 09:41:26 +01:00
|
|
|
|
|
|
|
this.listenFullScreenChange()
|
2019-01-23 15:36:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private runViewAdd () {
|
|
|
|
this.clearVideoViewInterval()
|
|
|
|
|
|
|
|
// After 30 seconds (or 3/4 of the video), add a view to the video
|
|
|
|
let minSecondsToView = 30
|
|
|
|
|
2020-12-07 15:58:57 +01:00
|
|
|
if (!this.isLive && this.videoDuration < minSecondsToView) {
|
|
|
|
minSecondsToView = (this.videoDuration * 3) / 4
|
|
|
|
}
|
2019-01-23 15:36:45 +01:00
|
|
|
|
|
|
|
let secondsViewed = 0
|
|
|
|
this.videoViewInterval = setInterval(() => {
|
|
|
|
if (this.player && !this.player.paused()) {
|
|
|
|
secondsViewed += 1
|
|
|
|
|
|
|
|
if (secondsViewed > minSecondsToView) {
|
2020-12-07 15:58:57 +01:00
|
|
|
// Restart the loop if this is a live
|
|
|
|
if (this.isLive) {
|
|
|
|
secondsViewed = 0
|
|
|
|
} else {
|
|
|
|
this.clearVideoViewInterval()
|
|
|
|
}
|
2019-01-23 15:36:45 +01:00
|
|
|
|
|
|
|
this.addViewToVideo().catch(err => console.error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 1000)
|
|
|
|
}
|
|
|
|
|
2021-03-31 11:26:32 +02:00
|
|
|
private runUserWatchVideo (options: UserWatching, videoUUID: string) {
|
2019-01-23 15:36:45 +01:00
|
|
|
let lastCurrentTime = 0
|
|
|
|
|
|
|
|
this.userWatchingVideoInterval = setInterval(() => {
|
|
|
|
const currentTime = Math.floor(this.player.currentTime())
|
|
|
|
|
|
|
|
if (currentTime - lastCurrentTime >= 1) {
|
|
|
|
lastCurrentTime = currentTime
|
|
|
|
|
2021-03-31 11:26:32 +02:00
|
|
|
if (options) {
|
|
|
|
this.notifyUserIsWatching(currentTime, options.url, options.authorizationHeader)
|
|
|
|
.catch(err => console.error('Cannot notify user is watching.', err))
|
|
|
|
} else {
|
|
|
|
saveVideoWatchHistory(videoUUID, currentTime)
|
|
|
|
}
|
2019-01-23 15:36:45 +01:00
|
|
|
}
|
|
|
|
}, this.CONSTANTS.USER_WATCHING_VIDEO_INTERVAL)
|
|
|
|
}
|
|
|
|
|
|
|
|
private clearVideoViewInterval () {
|
|
|
|
if (this.videoViewInterval !== undefined) {
|
|
|
|
clearInterval(this.videoViewInterval)
|
|
|
|
this.videoViewInterval = undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private addViewToVideo () {
|
|
|
|
if (!this.videoViewUrl) return Promise.resolve(undefined)
|
|
|
|
|
|
|
|
return fetch(this.videoViewUrl, { method: 'POST' })
|
|
|
|
}
|
|
|
|
|
|
|
|
private notifyUserIsWatching (currentTime: number, url: string, authorizationHeader: string) {
|
|
|
|
const body = new URLSearchParams()
|
|
|
|
body.append('currentTime', currentTime.toString())
|
|
|
|
|
2021-08-17 14:42:53 +02:00
|
|
|
const headers = new Headers({ Authorization: authorizationHeader })
|
2019-01-23 15:36:45 +01:00
|
|
|
|
|
|
|
return fetch(url, { method: 'PUT', body, headers })
|
|
|
|
}
|
|
|
|
|
2021-12-09 09:41:26 +01:00
|
|
|
private listenFullScreenChange () {
|
|
|
|
this.player.on('fullscreenchange', () => {
|
|
|
|
if (this.player.isFullscreen()) this.player.focus()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-07 10:17:19 +02:00
|
|
|
private listenControlBarMouse () {
|
2021-12-09 10:33:32 +01:00
|
|
|
const controlBar = this.player.controlBar
|
|
|
|
const settingsButton: SettingsButton = (controlBar as any).settingsButton
|
|
|
|
|
|
|
|
controlBar.on('mouseenter', () => {
|
2019-08-07 10:17:19 +02:00
|
|
|
this.mouseInControlBar = true
|
|
|
|
this.alterInactivity()
|
|
|
|
})
|
2019-01-23 15:36:45 +01:00
|
|
|
|
2021-12-09 10:33:32 +01:00
|
|
|
controlBar.on('mouseleave', () => {
|
2019-08-07 10:17:19 +02:00
|
|
|
this.mouseInControlBar = false
|
|
|
|
this.alterInactivity()
|
|
|
|
})
|
2021-12-09 10:33:32 +01:00
|
|
|
|
|
|
|
settingsButton.dialog.on('mouseenter', () => {
|
|
|
|
this.mouseInSettings = true
|
|
|
|
this.alterInactivity()
|
|
|
|
})
|
|
|
|
|
|
|
|
settingsButton.dialog.on('mouseleave', () => {
|
|
|
|
this.mouseInSettings = false
|
|
|
|
this.alterInactivity()
|
|
|
|
})
|
2019-08-07 10:17:19 +02:00
|
|
|
}
|
2019-01-23 15:36:45 +01:00
|
|
|
|
2019-08-07 10:17:19 +02:00
|
|
|
private alterInactivity () {
|
2021-12-09 10:33:32 +01:00
|
|
|
if (this.menuOpened || this.mouseInSettings || this.mouseInControlBar || this.isTouchEnabled()) {
|
|
|
|
this.setInactivityTimeout(0)
|
2019-08-07 10:17:19 +02:00
|
|
|
return
|
|
|
|
}
|
2019-01-23 15:36:45 +01:00
|
|
|
|
2021-12-09 10:33:32 +01:00
|
|
|
this.setInactivityTimeout(this.initialInactivityTimeout)
|
|
|
|
this.player.reportUserActivity(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
private setInactivityTimeout (timeout: number) {
|
|
|
|
(this.player as any).cache_.inactivityTimeout = timeout
|
|
|
|
this.player.options_.inactivityTimeout = timeout
|
2020-07-02 15:10:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private isTouchEnabled () {
|
|
|
|
return ('ontouchstart' in window) ||
|
|
|
|
navigator.maxTouchPoints > 0 ||
|
2021-11-10 16:14:16 +01:00
|
|
|
(navigator as any).msMaxTouchPoints > 0
|
2019-01-23 15:36:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private initCaptions () {
|
|
|
|
for (const caption of this.videoCaptions) {
|
|
|
|
this.player.addRemoteTextTrack({
|
|
|
|
kind: 'captions',
|
|
|
|
label: caption.label,
|
|
|
|
language: caption.language,
|
|
|
|
id: caption.language,
|
|
|
|
src: caption.src,
|
|
|
|
default: this.defaultSubtitle === caption.language
|
|
|
|
}, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.player.trigger('captionsChanged')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Thanks: https://github.com/videojs/video.js/issues/4460#issuecomment-312861657
|
|
|
|
private initSmoothProgressBar () {
|
2020-01-28 17:29:50 +01:00
|
|
|
const SeekBar = videojs.getComponent('SeekBar') as any
|
2019-01-23 15:36:45 +01:00
|
|
|
SeekBar.prototype.getPercent = function getPercent () {
|
|
|
|
// Allows for smooth scrubbing, when player can't keep up.
|
|
|
|
// const time = (this.player_.scrubbing()) ?
|
|
|
|
// this.player_.getCache().currentTime :
|
|
|
|
// this.player_.currentTime()
|
|
|
|
const time = this.player_.currentTime()
|
|
|
|
const percent = time / this.player_.duration()
|
|
|
|
return percent >= 1 ? 1 : percent
|
|
|
|
}
|
|
|
|
SeekBar.prototype.handleMouseMove = function handleMouseMove (event: any) {
|
|
|
|
let newTime = this.calculateDistance(event) * this.player_.duration()
|
|
|
|
if (newTime === this.player_.duration()) {
|
|
|
|
newTime = newTime - 0.1
|
|
|
|
}
|
|
|
|
this.player_.currentTime(newTime)
|
|
|
|
this.update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
videojs.registerPlugin('peertube', PeerTubePlugin)
|
|
|
|
export { PeerTubePlugin }
|