From 96cb4527f13f71024d27e2ac0383501b7b3b5a05 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 20 Feb 2019 11:26:14 +0100 Subject: [PATCH] Try to fix p2p-media-loader on ios --- .../p2p-media-loader-plugin.ts | 8 ++++++++ .../assets/player/peertube-player-manager.ts | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts b/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts index 022a9c16f..4dbfda300 100644 --- a/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts +++ b/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts @@ -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 }) diff --git a/client/src/assets/player/peertube-player-manager.ts b/client/src/assets/player/peertube-player-manager.ts index 0ba9bcb11..7631d095f 100644 --- a/client/src/assets/player/peertube-player-manager.ts +++ b/client/src/assets/player/peertube-player-manager.ts @@ -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