Try to fix p2p-media-loader on ios
authorChocobozzz <me@florianbigard.com>
Wed, 20 Feb 2019 10:26:14 +0000 (11:26 +0100)
committerChocobozzz <me@florianbigard.com>
Wed, 20 Feb 2019 10:26:14 +0000 (11:26 +0100)
client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
client/src/assets/player/peertube-player-manager.ts

index 022a9c16f51753d4e2fbfdbafd43b90ed6aa31f2..4dbfda30058cf147d82d7aef0c00d4b44b50a586 100644 (file)
@@ -40,6 +40,14 @@ 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
     })
index 0ba9bcb1142267422055d96eef196e34defa532f..7631d095fe3e51bdad8ac395037a17f02ecb9b3c 100644 (file)
@@ -116,12 +116,8 @@ export class PeertubePlayerManager {
       videojs(options.common.playerElement, videojsOptions, function (this: any) {
         const player = this
 
-        player.tech_.on('error', () => {
-          // Fallback to webtorrent?
-          if (mode === 'p2p-media-loader') {
-            self.fallbackToWebTorrent(player, options)
-          }
-        })
+        player.tech_.one('error', () => self.maybeFallbackToWebTorrent(mode, player, options))
+        player.one('error', () => self.maybeFallbackToWebTorrent(mode, player, options))
 
         self.addContextMenu(mode, player, options.common.embedUrl)
 
@@ -130,12 +126,19 @@ export class PeertubePlayerManager {
     })
   }
 
-  private static async fallbackToWebTorrent (player: any, options: PeertubePlayerManagerOptions) {
+  private static async maybeFallbackToWebTorrent (currentMode: PlayerMode, player: any, options: PeertubePlayerManagerOptions) {
+    if (currentMode === 'webtorrent') return
+
+    console.log('Fallback to webtorrent.')
+
     const newVideoElement = document.createElement('video')
     newVideoElement.className = this.playerElementClassName
 
     // VideoJS wraps our video element inside a div
-    const currentParentPlayerElement = options.common.playerElement.parentNode
+    let currentParentPlayerElement = options.common.playerElement.parentNode
+    // Fix on IOS, don't ask me why
+    if (!currentParentPlayerElement) currentParentPlayerElement = document.getElementById(options.common.playerElement.id).parentNode
+
     currentParentPlayerElement.parentNode.insertBefore(newVideoElement, currentParentPlayerElement)
 
     options.common.playerElement = newVideoElement