diff options
Diffstat (limited to 'client/src/assets/player')
-rw-r--r-- | client/src/assets/player/peertube-player-local-storage.ts | 8 | ||||
-rw-r--r-- | client/src/assets/player/peertube-videojs-plugin.ts | 48 |
2 files changed, 40 insertions, 16 deletions
diff --git a/client/src/assets/player/peertube-player-local-storage.ts b/client/src/assets/player/peertube-player-local-storage.ts index dac54c5a4..3ac5fe58a 100644 --- a/client/src/assets/player/peertube-player-local-storage.ts +++ b/client/src/assets/player/peertube-player-local-storage.ts | |||
@@ -10,6 +10,13 @@ function getStoredVolume () { | |||
10 | return undefined | 10 | return undefined |
11 | } | 11 | } |
12 | 12 | ||
13 | function getStoredWebTorrentEnabled (): boolean { | ||
14 | const value = getLocalStorage('webtorrent_enabled') | ||
15 | if (value !== null && value !== undefined) return value === 'true' | ||
16 | |||
17 | return false | ||
18 | } | ||
19 | |||
13 | function getStoredMute () { | 20 | function getStoredMute () { |
14 | const value = getLocalStorage('mute') | 21 | const value = getLocalStorage('mute') |
15 | if (value !== null && value !== undefined) return value === 'true' | 22 | if (value !== null && value !== undefined) return value === 'true' |
@@ -56,6 +63,7 @@ function getAverageBandwidthInStore () { | |||
56 | 63 | ||
57 | export { | 64 | export { |
58 | getStoredVolume, | 65 | getStoredVolume, |
66 | getStoredWebTorrentEnabled, | ||
59 | getStoredMute, | 67 | getStoredMute, |
60 | getStoredTheater, | 68 | getStoredTheater, |
61 | saveVolumeInStore, | 69 | saveVolumeInStore, |
diff --git a/client/src/assets/player/peertube-videojs-plugin.ts b/client/src/assets/player/peertube-videojs-plugin.ts index 2330f476f..5cebab6d9 100644 --- a/client/src/assets/player/peertube-videojs-plugin.ts +++ b/client/src/assets/player/peertube-videojs-plugin.ts | |||
@@ -11,11 +11,18 @@ import { | |||
11 | getAverageBandwidthInStore, | 11 | getAverageBandwidthInStore, |
12 | getStoredMute, | 12 | getStoredMute, |
13 | getStoredVolume, | 13 | getStoredVolume, |
14 | getStoredWebTorrentEnabled, | ||
14 | saveAverageBandwidth, | 15 | saveAverageBandwidth, |
15 | saveMuteInStore, | 16 | saveMuteInStore, |
16 | saveVolumeInStore | 17 | saveVolumeInStore |
17 | } from './peertube-player-local-storage' | 18 | } from './peertube-player-local-storage' |
18 | 19 | ||
20 | type PlayOptions = { | ||
21 | forcePlay?: boolean, | ||
22 | seek?: number, | ||
23 | delay?: number | ||
24 | } | ||
25 | |||
19 | const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') | 26 | const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') |
20 | class PeerTubePlugin extends Plugin { | 27 | class PeerTubePlugin extends Plugin { |
21 | private readonly playerElement: HTMLVideoElement | 28 | private readonly playerElement: HTMLVideoElement |
@@ -64,6 +71,7 @@ class PeerTubePlugin extends Plugin { | |||
64 | private autoResolution = true | 71 | private autoResolution = true |
65 | private forbidAutoResolution = false | 72 | private forbidAutoResolution = false |
66 | private isAutoResolutionObservation = false | 73 | private isAutoResolutionObservation = false |
74 | private playerRefusedP2P = false | ||
67 | 75 | ||
68 | private videoViewInterval | 76 | private videoViewInterval |
69 | private torrentInfoInterval | 77 | private torrentInfoInterval |
@@ -80,6 +88,7 @@ class PeerTubePlugin extends Plugin { | |||
80 | 88 | ||
81 | // Disable auto play on iOS | 89 | // Disable auto play on iOS |
82 | this.autoplay = options.autoplay && this.isIOS() === false | 90 | this.autoplay = options.autoplay && this.isIOS() === false |
91 | this.playerRefusedP2P = !getStoredWebTorrentEnabled() | ||
83 | 92 | ||
84 | this.startTime = timeToInt(options.startTime) | 93 | this.startTime = timeToInt(options.startTime) |
85 | this.videoFiles = options.videoFiles | 94 | this.videoFiles = options.videoFiles |
@@ -178,6 +187,15 @@ class PeerTubePlugin extends Plugin { | |||
178 | const previousVideoFile = this.currentVideoFile | 187 | const previousVideoFile = this.currentVideoFile |
179 | this.currentVideoFile = videoFile | 188 | this.currentVideoFile = videoFile |
180 | 189 | ||
190 | // Don't try on iOS that does not support MediaSource | ||
191 | // Or don't use P2P if webtorrent is disabled | ||
192 | if (this.isIOS() || this.playerRefusedP2P) { | ||
193 | return this.fallbackToHttp(options, () => { | ||
194 | this.player.playbackRate(oldPlaybackRate) | ||
195 | return done() | ||
196 | }) | ||
197 | } | ||
198 | |||
181 | this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, options, () => { | 199 | this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, options, () => { |
182 | this.player.playbackRate(oldPlaybackRate) | 200 | this.player.playbackRate(oldPlaybackRate) |
183 | return done() | 201 | return done() |
@@ -248,11 +266,7 @@ class PeerTubePlugin extends Plugin { | |||
248 | private addTorrent ( | 266 | private addTorrent ( |
249 | magnetOrTorrentUrl: string, | 267 | magnetOrTorrentUrl: string, |
250 | previousVideoFile: VideoFile, | 268 | previousVideoFile: VideoFile, |
251 | options: { | 269 | options: PlayOptions, |
252 | forcePlay?: boolean, | ||
253 | seek?: number, | ||
254 | delay?: number | ||
255 | }, | ||
256 | done: Function | 270 | done: Function |
257 | ) { | 271 | ) { |
258 | console.log('Adding ' + magnetOrTorrentUrl + '.') | 272 | console.log('Adding ' + magnetOrTorrentUrl + '.') |
@@ -288,7 +302,7 @@ class PeerTubePlugin extends Plugin { | |||
288 | renderVideo(torrent.files[ 0 ], this.playerElement, renderVideoOptions, (err, renderer) => { | 302 | renderVideo(torrent.files[ 0 ], this.playerElement, renderVideoOptions, (err, renderer) => { |
289 | this.renderer = renderer | 303 | this.renderer = renderer |
290 | 304 | ||
291 | if (err) return this.fallbackToHttp(done) | 305 | if (err) return this.fallbackToHttp(options, done) |
292 | 306 | ||
293 | return this.tryToPlay(err => { | 307 | return this.tryToPlay(err => { |
294 | if (err) return done(err) | 308 | if (err) return done(err) |
@@ -296,7 +310,7 @@ class PeerTubePlugin extends Plugin { | |||
296 | if (options.seek) this.seek(options.seek) | 310 | if (options.seek) this.seek(options.seek) |
297 | if (options.forcePlay === false && paused === true) this.player.pause() | 311 | if (options.forcePlay === false && paused === true) this.player.pause() |
298 | 312 | ||
299 | return done(err) | 313 | return done() |
300 | }) | 314 | }) |
301 | }) | 315 | }) |
302 | }, options.delay || 0) | 316 | }, options.delay || 0) |
@@ -432,12 +446,6 @@ class PeerTubePlugin extends Plugin { | |||
432 | return this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime }) | 446 | return this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime }) |
433 | } | 447 | } |
434 | 448 | ||
435 | // Don't try on iOS that does not support MediaSource | ||
436 | if (this.isIOS()) { | ||
437 | this.currentVideoFile = this.pickAverageVideoFile() | ||
438 | return this.fallbackToHttp(undefined, false) | ||
439 | } | ||
440 | |||
441 | // Proxy first play | 449 | // Proxy first play |
442 | const oldPlay = this.player.play.bind(this.player) | 450 | const oldPlay = this.player.play.bind(this.player) |
443 | this.player.play = () => { | 451 | this.player.play = () => { |
@@ -567,7 +575,9 @@ class PeerTubePlugin extends Plugin { | |||
567 | return fetch(url, { method: 'PUT', body, headers }) | 575 | return fetch(url, { method: 'PUT', body, headers }) |
568 | } | 576 | } |
569 | 577 | ||
570 | private fallbackToHttp (done?: Function, play = true) { | 578 | private fallbackToHttp (options: PlayOptions, done?: Function) { |
579 | const paused = this.player.paused() | ||
580 | |||
571 | this.disableAutoResolution(true) | 581 | this.disableAutoResolution(true) |
572 | 582 | ||
573 | this.flushVideoFile(this.currentVideoFile, true) | 583 | this.flushVideoFile(this.currentVideoFile, true) |
@@ -579,9 +589,15 @@ class PeerTubePlugin extends Plugin { | |||
579 | const httpUrl = this.currentVideoFile.fileUrl | 589 | const httpUrl = this.currentVideoFile.fileUrl |
580 | this.player.src = this.savePlayerSrcFunction | 590 | this.player.src = this.savePlayerSrcFunction |
581 | this.player.src(httpUrl) | 591 | this.player.src(httpUrl) |
582 | if (play) this.tryToPlay() | ||
583 | 592 | ||
584 | if (done) return done() | 593 | return this.tryToPlay(err => { |
594 | if (err && done) return done(err) | ||
595 | |||
596 | if (options.seek) this.seek(options.seek) | ||
597 | if (options.forcePlay === false && paused === true) this.player.pause() | ||
598 | |||
599 | if (done) return done() | ||
600 | }) | ||
585 | } | 601 | } |
586 | 602 | ||
587 | private handleError (err: Error | string) { | 603 | private handleError (err: Error | string) { |