X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fassets%2Fplayer%2Fpeertube-plugin.ts;h=07c7e33f6f8ff4ac004571f9e1d57da43437ffa4;hb=a45050e09edf6e2d74b6db48196f44ef9ce8fa58;hp=3991e462712455c8699c67078b5173993d6cc46d;hpb=f0a3988066f72a28bb44520af072f18d91d77dde;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/assets/player/peertube-plugin.ts b/client/src/assets/player/peertube-plugin.ts index 3991e4627..07c7e33f6 100644 --- a/client/src/assets/player/peertube-plugin.ts +++ b/client/src/assets/player/peertube-plugin.ts @@ -1,14 +1,10 @@ -// FIXME: something weird with our path definition in tsconfig and typings -// @ts-ignore -import * as videojs from 'video.js' +import videojs from 'video.js' import './videojs-components/settings-menu-button' import { PeerTubePluginOptions, ResolutionUpdateData, UserWatching, - VideoJSCaption, - VideoJSComponentInterface, - videojsUntyped + VideoJSCaption } from './peertube-videojs-typings' import { isMobile, timeToInt } from './utils' import { @@ -17,10 +13,12 @@ import { getStoredVolume, saveLastSubtitle, saveMuteInStore, + saveVideoWatchHistory, saveVolumeInStore } from './peertube-player-local-storage' -const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') +const Plugin = videojs.getPlugin('plugin') + class PeerTubePlugin extends Plugin { private readonly videoViewUrl: string private readonly videoDuration: number @@ -28,7 +26,6 @@ class PeerTubePlugin extends Plugin { USER_WATCHING_VIDEO_INTERVAL: 5000 // Every 5 seconds, notify the user is watching the video } - private player: any private videoCaptions: VideoJSCaption[] private defaultSubtitle: string @@ -36,14 +33,23 @@ class PeerTubePlugin extends Plugin { private userWatchingVideoInterval: any private lastResolutionChange: ResolutionUpdateData - constructor (player: videojs.Player, options: PeerTubePluginOptions) { - super(player, options) + private isLive: boolean + + private menuOpened = false + private mouseInControlBar = false + private readonly savedInactivityTimeout: number + + constructor (player: videojs.Player, options?: PeerTubePluginOptions) { + super(player) this.videoViewUrl = options.videoViewUrl this.videoDuration = options.videoDuration this.videoCaptions = options.videoCaptions + this.isLive = options.isLive - if (options.autoplay === true) this.player.addClass('vjs-has-autoplay') + this.savedInactivityTimeout = player.options_.inactivityTimeout + + if (options.autoplay) this.player.addClass('vjs-has-autoplay') this.player.on('autoplay-failure', () => { this.player.removeClass('vjs-has-autoplay') @@ -61,7 +67,7 @@ class PeerTubePlugin extends Plugin { this.player.p2pMediaLoader().on('resolutionChange', (_: any, d: any) => this.handleResolutionChange(d)) } - this.player.tech_.on('loadedqualitydata', () => { + this.player.tech(true).on('loadedqualitydata', () => { setTimeout(() => { // Replay a resolution change, now we loaded all quality data if (this.lastResolutionChange) this.handleResolutionChange(this.lastResolutionChange) @@ -83,14 +89,20 @@ class PeerTubePlugin extends Plugin { if (options.stopTime) { const stopTime = timeToInt(options.stopTime) + const self = this + + this.player.on('timeupdate', function onTimeUpdate () { + if (self.player.currentTime() > stopTime) { + self.player.pause() + self.player.trigger('stopped') - this.player.on('timeupdate', () => { - if (this.player.currentTime() > stopTime) this.player.pause() + self.player.off('timeupdate', onTimeUpdate) + } }) } this.player.textTracks().on('change', () => { - const showing = this.player.textTracks().tracks_.find((t: { kind: string, mode: string }) => { + const showing = this.player.textTracks().tracks_.find(t => { return t.kind === 'captions' && t.mode === 'showing' }) @@ -109,7 +121,7 @@ class PeerTubePlugin extends Plugin { this.initializePlayer() this.runViewAdd() - if (options.userWatching) this.runUserWatchVideo(options.userWatching) + this.runUserWatchVideo(options.userWatching, options.videoUUID) }) } @@ -118,6 +130,16 @@ class PeerTubePlugin extends Plugin { if (this.userWatchingVideoInterval) clearInterval(this.userWatchingVideoInterval) } + onMenuOpen () { + this.menuOpened = false + this.alterInactivity() + } + + onMenuClosed () { + this.menuOpened = true + this.alterInactivity() + } + private initializePlayer () { if (isMobile()) this.player.addClass('vjs-is-mobile') @@ -125,7 +147,7 @@ class PeerTubePlugin extends Plugin { this.initCaptions() - this.alterInactivity() + this.listenControlBarMouse() } private runViewAdd () { @@ -134,7 +156,9 @@ class PeerTubePlugin extends Plugin { // 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 + if (!this.isLive && this.videoDuration < minSecondsToView) { + minSecondsToView = (this.videoDuration * 3) / 4 + } let secondsViewed = 0 this.videoViewInterval = setInterval(() => { @@ -142,7 +166,12 @@ class PeerTubePlugin extends Plugin { secondsViewed += 1 if (secondsViewed > minSecondsToView) { - this.clearVideoViewInterval() + // Restart the loop if this is a live + if (this.isLive) { + secondsViewed = 0 + } else { + this.clearVideoViewInterval() + } this.addViewToVideo().catch(err => console.error(err)) } @@ -150,7 +179,7 @@ class PeerTubePlugin extends Plugin { }, 1000) } - private runUserWatchVideo (options: UserWatching) { + private runUserWatchVideo (options: UserWatching, videoUUID: string) { let lastCurrentTime = 0 this.userWatchingVideoInterval = setInterval(() => { @@ -159,8 +188,12 @@ class PeerTubePlugin extends Plugin { if (currentTime - lastCurrentTime >= 1) { lastCurrentTime = currentTime - this.notifyUserIsWatching(currentTime, options.url, options.authorizationHeader) - .catch(err => console.error('Cannot notify user is watching.', err)) + if (options) { + this.notifyUserIsWatching(currentTime, options.url, options.authorizationHeader) + .catch(err => console.error('Cannot notify user is watching.', err)) + } else { + saveVideoWatchHistory(videoUUID, currentTime) + } } }, this.CONSTANTS.USER_WATCHING_VIDEO_INTERVAL) } @@ -202,23 +235,33 @@ class PeerTubePlugin extends Plugin { this.trigger('resolutionChange', data) } - private alterInactivity () { - let saveInactivityTimeout: number + private listenControlBarMouse () { + this.player.controlBar.on('mouseenter', () => { + this.mouseInControlBar = true + this.alterInactivity() + }) - const disableInactivity = () => { - saveInactivityTimeout = this.player.options_.inactivityTimeout - this.player.options_.inactivityTimeout = 0 - } - const enableInactivity = () => { - this.player.options_.inactivityTimeout = saveInactivityTimeout + this.player.controlBar.on('mouseleave', () => { + this.mouseInControlBar = false + this.alterInactivity() + }) + } + + private alterInactivity () { + if (this.menuOpened) { + this.player.options_.inactivityTimeout = this.savedInactivityTimeout + return } - const settingsDialog = this.player.children_.find((c: any) => c.name_ === 'SettingsDialog') + if (!this.mouseInControlBar && !this.isTouchEnabled()) { + this.player.options_.inactivityTimeout = 1 + } + } - this.player.controlBar.on('mouseenter', () => disableInactivity()) - settingsDialog.on('mouseenter', () => disableInactivity()) - this.player.controlBar.on('mouseleave', () => enableInactivity()) - settingsDialog.on('mouseleave', () => enableInactivity()) + private isTouchEnabled () { + return ('ontouchstart' in window) || + navigator.maxTouchPoints > 0 || + navigator.msMaxTouchPoints > 0 } private initCaptions () { @@ -238,7 +281,7 @@ class PeerTubePlugin extends Plugin { // Thanks: https://github.com/videojs/video.js/issues/4460#issuecomment-312861657 private initSmoothProgressBar () { - const SeekBar = videojsUntyped.getComponent('SeekBar') + const SeekBar = videojs.getComponent('SeekBar') as any SeekBar.prototype.getPercent = function getPercent () { // Allows for smooth scrubbing, when player can't keep up. // const time = (this.player_.scrubbing()) ?