]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/peertube-plugin.ts
Migrate to @peertube/videojs-contextmenu
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-plugin.ts
index afd8d678b8258325da96bf86ea796b0c7a4d8864..9b4dc9bd58b8a722c16f0d1124ead6baa3fdfc8b 100644 (file)
@@ -1,20 +1,16 @@
-import videojs from 'video.js/dist/alt/video.core'
-import './videojs-components/settings-menu-button'
-import {
-  PeerTubePluginOptions,
-  ResolutionUpdateData,
-  UserWatching,
-  VideoJSCaption
-} from './peertube-videojs-typings'
-import { isMobile, timeToInt } from './utils'
+import videojs from 'video.js'
+import { timeToInt } from '@shared/core-utils'
 import {
   getStoredLastSubtitle,
   getStoredMute,
   getStoredVolume,
   saveLastSubtitle,
   saveMuteInStore,
+  saveVideoWatchHistory,
   saveVolumeInStore
 } from './peertube-player-local-storage'
+import { PeerTubePluginOptions, UserWatching, VideoJSCaption } from './peertube-videojs-typings'
+import { isMobile } from './utils'
 
 const Plugin = videojs.getPlugin('plugin')
 
@@ -30,7 +26,8 @@ class PeerTubePlugin extends Plugin {
 
   private videoViewInterval: any
   private userWatchingVideoInterval: any
-  private lastResolutionChange: ResolutionUpdateData
+
+  private isLive: boolean
 
   private menuOpened = false
   private mouseInControlBar = false
@@ -42,10 +39,11 @@ class PeerTubePlugin extends Plugin {
     this.videoViewUrl = options.videoViewUrl
     this.videoDuration = options.videoDuration
     this.videoCaptions = options.videoCaptions
+    this.isLive = options.isLive
 
     this.savedInactivityTimeout = player.options_.inactivityTimeout
 
-    if (options.autoplay === true) this.player.addClass('vjs-has-autoplay')
+    if (options.autoplay) this.player.addClass('vjs-has-autoplay')
 
     this.player.on('autoplay-failure', () => {
       this.player.removeClass('vjs-has-autoplay')
@@ -54,22 +52,6 @@ class PeerTubePlugin extends Plugin {
     this.player.ready(() => {
       const playerOptions = this.player.options_
 
-      if (options.mode === 'webtorrent') {
-        this.player.webtorrent().on('resolutionChange', (_: any, d: any) => this.handleResolutionChange(d))
-        this.player.webtorrent().on('autoResolutionChange', (_: any, d: any) => this.trigger('autoResolutionChange', d))
-      }
-
-      if (options.mode === 'p2p-media-loader') {
-        this.player.p2pMediaLoader().on('resolutionChange', (_: any, d: any) => this.handleResolutionChange(d))
-      }
-
-      this.player.tech(true).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)
 
@@ -97,7 +79,7 @@ class PeerTubePlugin extends Plugin {
         })
       }
 
-      this.player.textTracks().on('change', () => {
+      this.player.textTracks().addEventListener('change', () => {
         const showing = this.player.textTracks().tracks_.find(t => {
           return t.kind === 'captions' && t.mode === 'showing'
         })
@@ -117,7 +99,7 @@ class PeerTubePlugin extends Plugin {
       this.initializePlayer()
       this.runViewAdd()
 
-      if (options.userWatching) this.runUserWatchVideo(options.userWatching)
+      this.runUserWatchVideo(options.userWatching, options.videoUUID)
     })
   }
 
@@ -152,7 +134,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(() => {
@@ -160,7 +144,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))
         }
@@ -168,7 +157,7 @@ class PeerTubePlugin extends Plugin {
     }, 1000)
   }
 
-  private runUserWatchVideo (options: UserWatching) {
+  private runUserWatchVideo (options: UserWatching, videoUUID: string) {
     let lastCurrentTime = 0
 
     this.userWatchingVideoInterval = setInterval(() => {
@@ -177,8 +166,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)
   }
@@ -200,26 +193,11 @@ class PeerTubePlugin extends Plugin {
     const body = new URLSearchParams()
     body.append('currentTime', currentTime.toString())
 
-    const headers = new Headers({ 'Authorization': authorizationHeader })
+    const headers = new Headers({ Authorization: authorizationHeader })
 
     return fetch(url, { method: 'PUT', body, headers })
   }
 
-  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)
-  }
-
   private listenControlBarMouse () {
     this.player.controlBar.on('mouseenter', () => {
       this.mouseInControlBar = true
@@ -233,12 +211,20 @@ class PeerTubePlugin extends Plugin {
   }
 
   private alterInactivity () {
-    if (this.menuOpened || this.mouseInControlBar) {
+    if (this.menuOpened) {
       this.player.options_.inactivityTimeout = this.savedInactivityTimeout
       return
     }
 
-    this.player.options_.inactivityTimeout = 1
+    if (!this.mouseInControlBar && !this.isTouchEnabled()) {
+      this.player.options_.inactivityTimeout = 1
+    }
+  }
+
+  private isTouchEnabled () {
+    return ('ontouchstart' in window) ||
+      navigator.maxTouchPoints > 0 ||
+      navigator.msMaxTouchPoints > 0
   }
 
   private initCaptions () {