]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/webtorrent/webtorrent-plugin.ts
Fix webtorrent play on Safari
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / webtorrent / webtorrent-plugin.ts
index bf6b0a718dcbe024777916bf1ccf46ec091a27ea..85636c5be37e8323ca8b2251b187856131241b98 100644 (file)
@@ -1,9 +1,8 @@
-import videojs, { VideoJsPlayer } from 'video.js'
-
+import videojs from 'video.js'
 import * as WebTorrent from 'webtorrent'
 import { renderVideo } from './video-renderer'
 import { LoadedQualityData, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings'
-import { getRtcConfig, timeToInt, videoFileMaxByResolution, videoFileMinByResolution } from '../utils'
+import { getRtcConfig, timeToInt, videoFileMaxByResolution, videoFileMinByResolution, isIOS, isSafari } from '../utils'
 import { PeertubeChunkStore } from './peertube-chunk-store'
 import {
   getAverageBandwidthInStore,
@@ -31,7 +30,7 @@ class WebTorrentPlugin extends Plugin {
 
   private readonly autoplay: boolean = false
   private readonly startTime: number = 0
-  private readonly savePlayerSrcFunction: VideoJsPlayer['src']
+  private readonly savePlayerSrcFunction: videojs.Player['src']
   private readonly videoDuration: number
   private readonly CONSTANTS = {
     INFO_SCHEDULER: 1000, // Don't change this
@@ -69,13 +68,13 @@ class WebTorrentPlugin extends Plugin {
 
   private downloadSpeeds: number[] = []
 
-  constructor (player: VideoJsPlayer, options?: WebtorrentPluginOptions) {
+  constructor (player: videojs.Player, options?: WebtorrentPluginOptions) {
     super(player)
 
     this.startTime = timeToInt(options.startTime)
 
     // Disable auto play on iOS
-    this.autoplay = options.autoplay && this.isIOS() === false
+    this.autoplay = options.autoplay && isIOS() === false
     this.playerRefusedP2P = !getStoredP2PEnabled()
 
     this.videoFiles = options.videoFiles
@@ -159,7 +158,7 @@ class WebTorrentPlugin extends Plugin {
 
     // Don't try on iOS that does not support MediaSource
     // Or don't use P2P if webtorrent is disabled
-    if (this.isIOS() || this.playerRefusedP2P) {
+    if (isIOS() || this.playerRefusedP2P) {
       return this.fallbackToHttp(options, () => {
         this.player.playbackRate(oldPlaybackRate)
         return done()
@@ -330,6 +329,11 @@ class WebTorrentPlugin extends Plugin {
   private tryToPlay (done?: (err?: Error) => void) {
     if (!done) done = function () { /* empty */ }
 
+    // Try in mute mode because we have issues with Safari
+    if (isSafari() && this.player.muted() === false) {
+      this.player.muted(true)
+    }
+
     const playPromise = this.player.play()
     if (playPromise !== undefined) {
       return playPromise.then(() => done())
@@ -418,7 +422,7 @@ class WebTorrentPlugin extends Plugin {
   private initializePlayer () {
     this.buildQualities()
 
-    if (this.autoplay === true) {
+    if (this.autoplay) {
       this.player.posterImage.hide()
 
       return this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime })
@@ -544,10 +548,6 @@ class WebTorrentPlugin extends Plugin {
     this.player.removeClass('vjs-error-display-enabled')
   }
 
-  private isIOS () {
-    return !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)
-  }
-
   private pickAverageVideoFile () {
     if (this.videoFiles.length === 1) return this.videoFiles[0]