PeerTube/client/src/assets/player/peertube-plugin.ts

288 lines
8.4 KiB
TypeScript
Raw Normal View History

// FIXME: something weird with our path definition in tsconfig and typings
// @ts-ignore
import * as videojs from 'video.js'
import './videojs-components/settings-menu-button'
2019-01-24 10:16:30 +01:00
import {
PeerTubePluginOptions,
ResolutionUpdateData,
UserWatching,
VideoJSCaption,
VideoJSComponentInterface,
videojsUntyped
} from './peertube-videojs-typings'
import { isMobile, timeToInt } from './utils'
import {
getStoredLastSubtitle,
getStoredMute,
getStoredVolume,
saveLastSubtitle,
saveMuteInStore,
saveVolumeInStore
} from './peertube-player-local-storage'
const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin')
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 player: any
private videoCaptions: VideoJSCaption[]
private defaultSubtitle: string
private videoViewInterval: any
private userWatchingVideoInterval: any
2019-01-24 10:16:30 +01:00
private lastResolutionChange: ResolutionUpdateData
2019-08-07 10:17:19 +02:00
private menuOpened = false
private mouseInControlBar = false
private readonly savedInactivityTimeout: number
constructor (player: videojs.Player, options: PeerTubePluginOptions) {
super(player, options)
this.videoViewUrl = options.videoViewUrl
this.videoDuration = options.videoDuration
this.videoCaptions = options.videoCaptions
2019-08-07 10:17:19 +02:00
this.savedInactivityTimeout = player.options_.inactivityTimeout
2019-02-06 10:39:50 +01:00
if (options.autoplay === true) this.player.addClass('vjs-has-autoplay')
this.player.on('autoplay-failure', () => {
this.player.removeClass('vjs-has-autoplay')
})
this.player.ready(() => {
const playerOptions = this.player.options_
2019-01-29 08:37:25 +01:00
if (options.mode === 'webtorrent') {
2019-01-24 10:16:30 +01:00
this.player.webtorrent().on('resolutionChange', (_: any, d: any) => this.handleResolutionChange(d))
this.player.webtorrent().on('autoResolutionChange', (_: any, d: any) => this.trigger('autoResolutionChange', d))
}
2019-01-29 08:37:25 +01:00
if (options.mode === 'p2p-media-loader') {
2019-01-24 10:16:30 +01:00
this.player.p2pMediaLoader().on('resolutionChange', (_: any, d: any) => this.handleResolutionChange(d))
}
this.player.tech_.on('loadedqualitydata', () => {
setTimeout(() => {
// Replay a resolution change, now we loaded all quality data
if (this.lastResolutionChange) this.handleResolutionChange(this.lastResolutionChange)
}, 0)
})
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
})
}
this.player.textTracks().on('change', () => {
const showing = this.player.textTracks().tracks_.find((t: { kind: string, mode: string }) => {
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()
if (options.userWatching) this.runUserWatchVideo(options.userWatching)
})
}
dispose () {
2019-03-07 17:06:00 +01:00
if (this.videoViewInterval) clearInterval(this.videoViewInterval)
if (this.userWatchingVideoInterval) clearInterval(this.userWatchingVideoInterval)
}
2019-08-07 10:17:19 +02:00
onMenuOpen () {
this.menuOpened = false
this.alterInactivity()
}
onMenuClosed () {
this.menuOpened = true
this.alterInactivity()
}
private initializePlayer () {
if (isMobile()) this.player.addClass('vjs-is-mobile')
this.initSmoothProgressBar()
this.initCaptions()
2019-08-07 10:17:19 +02:00
this.listenControlBarMouse()
}
private runViewAdd () {
this.clearVideoViewInterval()
// After 30 seconds (or 3/4 of the video), add a view to the video
let minSecondsToView = 30
if (this.videoDuration < minSecondsToView) minSecondsToView = (this.videoDuration * 3) / 4
let secondsViewed = 0
this.videoViewInterval = setInterval(() => {
if (this.player && !this.player.paused()) {
secondsViewed += 1
if (secondsViewed > minSecondsToView) {
this.clearVideoViewInterval()
this.addViewToVideo().catch(err => console.error(err))
}
}
}, 1000)
}
private runUserWatchVideo (options: UserWatching) {
let lastCurrentTime = 0
this.userWatchingVideoInterval = setInterval(() => {
const currentTime = Math.floor(this.player.currentTime())
if (currentTime - lastCurrentTime >= 1) {
lastCurrentTime = currentTime
this.notifyUserIsWatching(currentTime, options.url, options.authorizationHeader)
.catch(err => console.error('Cannot notify user is watching.', err))
}
}, 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())
const headers = new Headers({ 'Authorization': authorizationHeader })
return fetch(url, { method: 'PUT', body, headers })
}
2019-01-24 10:16:30 +01:00
private handleResolutionChange (data: ResolutionUpdateData) {
this.lastResolutionChange = data
const qualityLevels = this.player.qualityLevels()
for (let i = 0; i < qualityLevels.length; i++) {
if (qualityLevels[i].height === data.resolutionId) {
data.id = qualityLevels[i].id
break
}
}
this.trigger('resolutionChange', data)
}
2019-08-07 10:17:19 +02:00
private listenControlBarMouse () {
this.player.controlBar.on('mouseenter', () => {
this.mouseInControlBar = true
this.alterInactivity()
})
2019-08-07 10:17:19 +02:00
this.player.controlBar.on('mouseleave', () => {
this.mouseInControlBar = false
this.alterInactivity()
})
}
2019-08-07 10:17:19 +02:00
private alterInactivity () {
if (this.menuOpened || this.mouseInControlBar) {
this.player.options_.inactivityTimeout = this.savedInactivityTimeout
return
}
2019-08-07 10:17:19 +02:00
this.player.options_.inactivityTimeout = 1
}
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 () {
const SeekBar = videojsUntyped.getComponent('SeekBar')
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 }