]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/peertube-videojs-plugin.ts
Fix video play promise error on non supported browsers
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-videojs-plugin.ts
index 34c993f3c2a37b3f4ed39a4d36204b3bc017be59..01a630cb62cc4c12951b29f4078fa5eb38397b7c 100644 (file)
@@ -1,26 +1,31 @@
 // Big thanks to: https://github.com/kmoskwiak/videojs-resolution-switcher
 
+import { VideoService } from '@app/shared/video/video.service'
 import * as videojs from 'video.js'
 import * as WebTorrent from 'webtorrent'
 import { VideoFile } from '../../../../shared/models/videos/video.model'
 import { renderVideo } from './video-renderer'
 
+declare module 'video.js' {
+  interface Player {
+    peertube (): PeerTubePlugin
+  }
+}
+
 interface VideoJSComponentInterface {
-  _player: VideoJSPlayer
+  _player: videojs.Player
 
-  new (player: VideoJSPlayer, options?: any)
+  new (player: videojs.Player, options?: any)
 
   registerComponent (name: string, obj: any)
 }
 
-interface VideoJSPlayer extends videojs.Player {
-  peertube (): PeerTubePlugin
-}
-
 type PeertubePluginOptions = {
   videoFiles: VideoFile[]
   playerElement: HTMLVideoElement
   peerTubeLink: boolean
+  videoViewUrl: string
+  videoDuration: number
 }
 
 // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
@@ -45,7 +50,7 @@ const webtorrent = new WebTorrent({ dht: false })
 const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem')
 class ResolutionMenuItem extends MenuItem {
 
-  constructor (player: VideoJSPlayer, options) {
+  constructor (player: videojs.Player, options) {
     options.selectable = true
     super(player, options)
 
@@ -54,7 +59,8 @@ class ResolutionMenuItem extends MenuItem {
   }
 
   handleClick (event) {
-    MenuItem.prototype.handleClick.call(this, event)
+    super.handleClick(event)
+
     this.player_.peertube().updateResolution(this.options_.id)
   }
 }
@@ -64,7 +70,7 @@ const MenuButton: VideoJSComponentInterface = videojsUntyped.getComponent('MenuB
 class ResolutionMenuButton extends MenuButton {
   label: HTMLElement
 
-  constructor (player: VideoJSPlayer, options) {
+  constructor (player: videojs.Player, options) {
     options.label = 'Quality'
     super(player, options)
 
@@ -214,11 +220,22 @@ class PeerTubePlugin extends Plugin {
   private playerElement: HTMLVideoElement
   private videoFiles: VideoFile[]
   private torrent: WebTorrent.Torrent
+  private autoplay = false
+  private videoViewUrl: string
+  private videoDuration: number
+  private videoViewInterval
+  private torrentInfoInterval
 
-  constructor (player: VideoJSPlayer, options: PeertubePluginOptions) {
+  constructor (player: videojs.Player, options: PeertubePluginOptions) {
     super(player, options)
 
+    // Fix canplay event on google chrome by disabling default videojs autoplay
+    this.autoplay = this.player.options_.autoplay
+    this.player.options_.autoplay = false
+
     this.videoFiles = options.videoFiles
+    this.videoViewUrl = options.videoViewUrl
+    this.videoDuration = options.videoDuration
 
     // Hack to "simulate" src link in video.js >= 6
     // Without this, we can't play the video after pausing it
@@ -232,10 +249,14 @@ class PeerTubePlugin extends Plugin {
     this.player.ready(() => {
       this.initializePlayer(options)
       this.runTorrentInfoScheduler()
+      this.runViewAdd()
     })
   }
 
   dispose () {
+    clearInterval(this.videoViewInterval)
+    clearInterval(this.torrentInfoInterval)
+
     // Don't need to destroy renderer, video player will be destroyed
     this.flushVideoFile(this.currentVideoFile, false)
   }
@@ -277,14 +298,21 @@ class PeerTubePlugin extends Plugin {
         if (err) return this.handleError(err)
 
         this.renderer = renderer
-        this.player.play().then(done)
+        if (!this.player.paused()) {
+          const playPromise = this.player.play()
+          if (playPromise !== undefined) return playPromise.then(done)
+
+          return done()
+        }
+
+        return done()
       })
     })
 
     this.torrent.on('error', err => this.handleError(err))
     this.torrent.on('warning', (err: any) => {
       // We don't support HTTP tracker but we don't care -> we use the web socket tracker
-      if (err.message.indexOf('Unsupported tracker protocol: http') !== -1) return
+      if (err.message.indexOf('Unsupported tracker protocol') !== -1) return
       // Users don't care about issues with WebRTC, but developers do so log it in the console
       if (err.message.indexOf('Ice connection failed') !== -1) {
         console.error(err)
@@ -302,6 +330,9 @@ class PeerTubePlugin extends Plugin {
     const currentTime = this.player.currentTime()
     const isPaused = this.player.paused()
 
+    // Remove poster to have black background
+    this.playerElement.poster = ''
+
     // Hide bigPlayButton
     if (!isPaused) {
       this.player.bigPlayButton.hide()
@@ -322,9 +353,13 @@ class PeerTubePlugin extends Plugin {
     }
   }
 
-  setVideoFiles (files: VideoFile[]) {
+  setVideoFiles (files: VideoFile[], videoViewUrl: string, videoDuration: number) {
+    this.videoViewUrl = videoViewUrl
+    this.videoDuration = videoDuration
     this.videoFiles = files
 
+    // Re run view add for the new video
+    this.runViewAdd()
     this.updateVideoFile(undefined, () => this.player.play())
   }
 
@@ -343,24 +378,18 @@ class PeerTubePlugin extends Plugin {
     const webTorrentButton = new WebTorrentButton(this.player)
     controlBar.webTorrent = controlBar.el().insertBefore(webTorrentButton.el(), controlBar.progressControl.el())
 
-    if (this.player.options_.autoplay === true) {
-      this.updateVideoFile()
+    if (this.autoplay === true) {
+      this.updateVideoFile(undefined, () => this.player.play())
     } else {
       this.player.one('play', () => {
-        // On firefox, we need to wait to load the video before playing
-        if (navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) {
-          this.player.pause()
-          this.updateVideoFile(undefined, () => this.player.play())
-          return
-        }
-
-        this.updateVideoFile(undefined)
+        this.player.pause()
+        this.updateVideoFile(undefined, () => this.player.play())
       })
     }
   }
 
   private runTorrentInfoScheduler () {
-    setInterval(() => {
+    this.torrentInfoInterval = setInterval(() => {
       if (this.torrent !== undefined) {
         this.trigger('torrentInfo', {
           downloadSpeed: this.torrent.downloadSpeed,
@@ -371,6 +400,39 @@ class PeerTubePlugin extends Plugin {
     }, 1000)
   }
 
+  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 clearVideoViewInterval () {
+    if (this.videoViewInterval !== undefined) {
+      clearInterval(this.videoViewInterval)
+      this.videoViewInterval = undefined
+    }
+  }
+
+  private addViewToVideo () {
+    return fetch(this.videoViewUrl, { method: 'POST' })
+  }
+
   private handleError (err: Error | string) {
     return this.player.trigger('customError', { err })
   }