]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/peertube-videojs-plugin.ts
Merge branch 'develop' of framagit.org:chocobozzz/PeerTube into develop
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-videojs-plugin.ts
index 2eddcb1d8dc828d2f120505767fd46601e3f3145..290d88724f063bbd914b783332f56f54ca5d3a69 100644 (file)
@@ -36,6 +36,7 @@ class PeerTubePlugin extends Plugin {
   private readonly playerElement: HTMLVideoElement
 
   private readonly autoplay: boolean = false
+  private readonly startTime: number = 0
   private readonly savePlayerSrcFunction: Function
   private readonly videoFiles: VideoFile[]
   private readonly videoViewUrl: string
@@ -67,10 +68,9 @@ class PeerTubePlugin extends Plugin {
   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.autoplay = options.autoplay
 
+    this.startTime = options.startTime
     this.videoFiles = options.videoFiles
     this.videoViewUrl = options.videoViewUrl
     this.videoDuration = options.videoDuration
@@ -83,6 +83,8 @@ class PeerTubePlugin extends Plugin {
 
     this.playerElement = options.playerElement
 
+    if (this.autoplay === true) this.player.addClass('vjs-has-autoplay')
+
     this.player.ready(() => {
       const volume = getStoredVolume()
       if (volume !== undefined) this.player.volume(volume)
@@ -94,7 +96,7 @@ class PeerTubePlugin extends Plugin {
       this.runViewAdd()
 
       this.player.one('play', () => {
-        // Don't run immediately scheduler, wait some seconds the TCP connections are maid
+        // Don't run immediately scheduler, wait some seconds the TCP connections are made
         this.runAutoQualitySchedulerTimer = setTimeout(() => {
           this.runAutoQualityScheduler()
         }, this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
@@ -186,12 +188,7 @@ class PeerTubePlugin extends Plugin {
 
           if (err) return this.fallbackToHttp(done)
 
-          if (!this.player.paused()) {
-            const playPromise = this.player.play()
-            if (playPromise !== undefined) return playPromise.then(done)
-
-            return done()
-          }
+          if (!this.player.paused()) return this.tryToPlay(done)
 
           return done()
         })
@@ -234,10 +231,7 @@ class PeerTubePlugin extends Plugin {
     }
 
     const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId)
-    this.updateVideoFile(newVideoFile, delay, () => {
-      this.player.currentTime(currentTime)
-      this.player.handleTechSeeked_()
-    })
+    this.updateVideoFile(newVideoFile, delay, () => this.seek(currentTime))
   }
 
   flushVideoFile (videoFile: VideoFile, destroyRenderer = true) {
@@ -263,6 +257,30 @@ class PeerTubePlugin extends Plugin {
     this.trigger('autoResolutionUpdate')
   }
 
+  private tryToPlay (done?: Function) {
+    if (!done) done = function () { /* empty */ }
+
+    const playPromise = this.player.play()
+    if (playPromise !== undefined) {
+      return playPromise.then(done)
+                        .catch(err => {
+                          console.error(err)
+                          this.player.pause()
+                          this.player.posterImage.show()
+                          this.player.removeClass('vjs-has-autoplay')
+
+                          return done()
+                        })
+    }
+
+    return done()
+  }
+
+  private seek (time: number) {
+    this.player.currentTime(time)
+    this.player.handleTechSeeked_()
+  }
+
   private getAppropriateFile (averageDownloadSpeed?: number): VideoFile {
     if (this.videoFiles === undefined || this.videoFiles.length === 0) return undefined
     if (this.videoFiles.length === 1) return this.videoFiles[0]
@@ -310,13 +328,19 @@ class PeerTubePlugin extends Plugin {
 
     if (this.autoplay === true) {
       this.player.posterImage.hide()
-      this.updateVideoFile(undefined, 0, () => this.player.play())
+
+      this.updateVideoFile(undefined, 0, () => {
+        this.seek(this.startTime)
+        this.tryToPlay()
+      })
     } else {
       // Proxy first play
       const oldPlay = this.player.play.bind(this.player)
       this.player.play = () => {
-        this.updateVideoFile(undefined, 0, () => oldPlay)
+        this.player.addClass('vjs-has-big-play-button-clicked')
         this.player.play = oldPlay
+
+        this.updateVideoFile(undefined, 0, () => this.seek(this.startTime))
       }
     }
   }