]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
Merge branch 'release/3.4.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / p2p-media-loader / p2p-media-loader-plugin.ts
index 46c6bbaf2861b58c2fd2c00cd4810fff03b37172..d917fda038ff0ab478790893f261225c624295ed 100644 (file)
@@ -1,10 +1,10 @@
+import Hlsjs from 'hls.js'
 import videojs from 'video.js'
+import { Events, Segment } from '@peertube/p2p-media-loader-core'
+import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs'
+import { timeToInt } from '@shared/core-utils'
 import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings'
-import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs'
-import { Events, Segment } from 'p2p-media-loader-core'
-import { timeToInt } from '../utils'
 import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
-import * as Hlsjs from 'hls.js/dist/hls.light.js'
 
 registerConfigPlugin(videojs)
 registerSourceHandler(videojs)
@@ -43,19 +43,23 @@ class P2pMediaLoaderPlugin extends Plugin {
 
     // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
     if (!(videojs as any).Html5Hlsjs) {
-      const message = 'HLS.js does not seem to be supported.'
-      console.warn(message)
+      console.warn('HLS.js does not seem to be supported. Try to fallback to built in HLS.')
 
-      player.ready(() => player.trigger('error', new Error(message)))
-      return
-    }
+      if (!player.canPlayType('application/vnd.apple.mpegurl')) {
+        const message = 'Cannot fallback to built-in HLS'
+        console.warn(message)
 
-    // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
-    (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
-      this.hlsjs = hlsjs
-    })
+        player.ready(() => player.trigger('error', new Error(message)))
+        return
+      }
+    } else {
+      // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
+      (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
+        this.hlsjs = hlsjs
+      })
 
-    initVideoJsContribHlsJsPlayer(player)
+      initVideoJsContribHlsJsPlayer(player)
+    }
 
     this.startTime = timeToInt(options.startTime)
 
@@ -64,11 +68,13 @@ class P2pMediaLoaderPlugin extends Plugin {
       src: options.src
     })
 
-    player.one('play', () => {
-      player.addClass('vjs-has-big-play-button-clicked')
-    })
+    player.ready(() => {
+      this.initializeCore()
 
-    player.ready(() => this.initialize())
+      if ((videojs as any).Html5Hlsjs) {
+        this.initializePlugin()
+      }
+    })
   }
 
   dispose () {
@@ -78,21 +84,38 @@ class P2pMediaLoaderPlugin extends Plugin {
     clearInterval(this.networkInfoInterval)
   }
 
+  getCurrentLevel () {
+    return this.hlsjs.levels[this.hlsjs.currentLevel]
+  }
+
+  getLiveLatency () {
+    // FIXME: typings
+    return Math.round((this.hlsjs as any).latency)
+  }
+
   getHLSJS () {
     return this.hlsjs
   }
 
-  private initialize () {
+  private initializeCore () {
+    this.player.one('play', () => {
+      this.player.addClass('vjs-has-big-play-button-clicked')
+    })
+
+    this.player.one('canplay', () => {
+      if (this.startTime) {
+        this.player.currentTime(this.startTime)
+      }
+    })
+  }
+
+  private initializePlugin () {
     initHlsJsPlayer(this.hlsjs)
 
     // FIXME: typings
     const options = this.player.tech(true).options_ as any
     this.p2pEngine = options.hlsjsConfig.loader.getEngine()
 
-    this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHING, (_: any, data: any) => {
-      this.trigger('resolutionChange', { auto: this.hlsjs.autoLevelEnabled, resolutionId: data.height })
-    })
-
     this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => {
       console.error('Segment error.', segment, err)
 
@@ -102,27 +125,21 @@ class P2pMediaLoaderPlugin extends Plugin {
     this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
 
     this.runStats()
-
-    this.player.one('canplay', () => {
-      if (this.startTime) {
-        this.player.currentTime(this.startTime)
-      }
-    })
   }
 
   private runStats () {
-    this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, size: number) => {
+    this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, _segment, bytes: number) => {
       const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
 
-      elem.pendingDownload.push(size)
-      elem.totalDownload += size
+      elem.pendingDownload.push(bytes)
+      elem.totalDownload += bytes
     })
 
-    this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, size: number) => {
+    this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => {
       const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
 
-      elem.pendingUpload.push(size)
-      elem.totalUpload += size
+      elem.pendingUpload.push(bytes)
+      elem.totalUpload += bytes
     })
 
     this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
@@ -141,6 +158,7 @@ class P2pMediaLoaderPlugin extends Plugin {
       this.statsHTTPBytes.pendingUpload = []
 
       return this.player.trigger('p2pInfo', {
+        source: 'p2p-media-loader',
         http: {
           downloadSpeed: httpDownloadSpeed,
           uploadSpeed: httpUploadSpeed,
@@ -153,7 +171,8 @@ class P2pMediaLoaderPlugin extends Plugin {
           numPeers: this.statsP2PBytes.numPeers,
           downloaded: this.statsP2PBytes.totalDownload,
           uploaded: this.statsP2PBytes.totalUpload
-        }
+        },
+        bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8
       } as PlayerNetworkInfo)
     }, this.CONSTANTS.INFO_SCHEDULER)
   }