]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
Improve HLS redundancy
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / p2p-media-loader / p2p-media-loader-plugin.ts
index f9a2707fb881f1b71cc1234e2f3649840547cd4c..0c8c612ee8834eeb47ba0fcf1eeb73d71073d845 100644 (file)
@@ -3,7 +3,8 @@
 import * as videojs from 'video.js'
 import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo, VideoJSComponentInterface } from '../peertube-videojs-typings'
 import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs'
-import { Events } from 'p2p-media-loader-core'
+import { Events, Segment } from 'p2p-media-loader-core'
+import { timeToInt } from '../utils'
 
 // videojs-hlsjs-plugin needs videojs in window
 window['videojs'] = videojs
@@ -32,6 +33,7 @@ class P2pMediaLoaderPlugin extends Plugin {
     totalDownload: 0,
     totalUpload: 0
   }
+  private startTime: number
 
   private networkInfoInterval: any
 
@@ -40,28 +42,46 @@ class P2pMediaLoaderPlugin extends Plugin {
 
     this.options = options
 
+    if (!videojs.Html5Hlsjs) {
+      const message = 'HLS.js does not seem to be supported.'
+      console.warn(message)
+
+      player.ready(() => player.trigger('error', new Error(message)))
+      return
+    }
+
     videojs.Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
       this.hlsjs = hlsjs
     })
 
     initVideoJsContribHlsJsPlayer(player)
 
+    this.startTime = timeToInt(options.startTime)
+
     player.src({
       type: options.type,
       src: options.src
     })
 
+    player.one('play', () => {
+      player.addClass('vjs-has-big-play-button-clicked')
+    })
+
     player.ready(() => this.initialize())
   }
 
   dispose () {
+    if (this.hlsjs) this.hlsjs.destroy()
+    if (this.p2pEngine) this.p2pEngine.destroy()
+
     clearInterval(this.networkInfoInterval)
   }
 
   private initialize () {
     initHlsJsPlayer(this.hlsjs)
 
-    this.p2pEngine = this.player.tech_.options_.hlsjsConfig.loader.getEngine()
+    const tech = this.player.tech_
+    this.p2pEngine = tech.options_.hlsjsConfig.loader.getEngine()
 
     // Avoid using constants to not import hls.hs
     // https://github.com/video-dev/hls.js/blob/master/src/events.js#L37
@@ -69,13 +89,21 @@ class P2pMediaLoaderPlugin extends Plugin {
       this.trigger('resolutionChange', { auto: this.hlsjs.autoLevelEnabled, resolutionId: data.height })
     })
 
-    this.p2pEngine.on(Events.SegmentError, (segment, err) => {
+    this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => {
       console.error('Segment error.', segment, err)
+
+      this.options.redundancyUrlManager.removeByOriginUrl(segment.url)
     })
 
-    this.statsP2PBytes.numPeers = 1 + this.options.redundancyBaseUrls.length
+    this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
 
     this.runStats()
+
+    this.player.one('canplay', () => {
+      if (this.startTime) {
+        this.player.currentTime(this.startTime)
+      }
+    })
   }
 
   private runStats () {