diff options
Diffstat (limited to 'client/src/assets/player/webtorrent/webtorrent-plugin.ts')
-rw-r--r-- | client/src/assets/player/webtorrent/webtorrent-plugin.ts | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/client/src/assets/player/webtorrent/webtorrent-plugin.ts b/client/src/assets/player/webtorrent/webtorrent-plugin.ts index 35cf85c99..d26fc38fa 100644 --- a/client/src/assets/player/webtorrent/webtorrent-plugin.ts +++ b/client/src/assets/player/webtorrent/webtorrent-plugin.ts | |||
@@ -1,17 +1,15 @@ | |||
1 | // FIXME: something weird with our path definition in tsconfig and typings | 1 | import videojs, { VideoJsPlayer } from 'video.js' |
2 | // @ts-ignore | ||
3 | import * as videojs from 'video.js' | ||
4 | 2 | ||
5 | import * as WebTorrent from 'webtorrent' | 3 | import * as WebTorrent from 'webtorrent' |
6 | import { renderVideo } from './video-renderer' | 4 | import { renderVideo } from './video-renderer' |
7 | import { LoadedQualityData, PlayerNetworkInfo, VideoJSComponentInterface, WebtorrentPluginOptions } from '../peertube-videojs-typings' | 5 | import { LoadedQualityData, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings' |
8 | import { getRtcConfig, timeToInt, videoFileMaxByResolution, videoFileMinByResolution } from '../utils' | 6 | import { getRtcConfig, timeToInt, videoFileMaxByResolution, videoFileMinByResolution } from '../utils' |
9 | import { PeertubeChunkStore } from './peertube-chunk-store' | 7 | import { PeertubeChunkStore } from './peertube-chunk-store' |
10 | import { | 8 | import { |
11 | getAverageBandwidthInStore, | 9 | getAverageBandwidthInStore, |
12 | getStoredMute, | 10 | getStoredMute, |
13 | getStoredVolume, | ||
14 | getStoredP2PEnabled, | 11 | getStoredP2PEnabled, |
12 | getStoredVolume, | ||
15 | saveAverageBandwidth | 13 | saveAverageBandwidth |
16 | } from '../peertube-player-local-storage' | 14 | } from '../peertube-player-local-storage' |
17 | import { VideoFile } from '@shared/models' | 15 | import { VideoFile } from '@shared/models' |
@@ -24,13 +22,14 @@ type PlayOptions = { | |||
24 | delay?: number | 22 | delay?: number |
25 | } | 23 | } |
26 | 24 | ||
27 | const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') | 25 | const Plugin = videojs.getPlugin('plugin') |
26 | |||
28 | class WebTorrentPlugin extends Plugin { | 27 | class WebTorrentPlugin extends Plugin { |
29 | private readonly playerElement: HTMLVideoElement | 28 | private readonly playerElement: HTMLVideoElement |
30 | 29 | ||
31 | private readonly autoplay: boolean = false | 30 | private readonly autoplay: boolean = false |
32 | private readonly startTime: number = 0 | 31 | private readonly startTime: number = 0 |
33 | private readonly savePlayerSrcFunction: Function | 32 | private readonly savePlayerSrcFunction: VideoJsPlayer['src'] |
34 | private readonly videoFiles: VideoFile[] | 33 | private readonly videoFiles: VideoFile[] |
35 | private readonly videoDuration: number | 34 | private readonly videoDuration: number |
36 | private readonly CONSTANTS = { | 35 | private readonly CONSTANTS = { |
@@ -49,7 +48,6 @@ class WebTorrentPlugin extends Plugin { | |||
49 | dht: false | 48 | dht: false |
50 | }) | 49 | }) |
51 | 50 | ||
52 | private player: any | ||
53 | private currentVideoFile: VideoFile | 51 | private currentVideoFile: VideoFile |
54 | private torrent: WebTorrent.Torrent | 52 | private torrent: WebTorrent.Torrent |
55 | 53 | ||
@@ -70,8 +68,8 @@ class WebTorrentPlugin extends Plugin { | |||
70 | 68 | ||
71 | private downloadSpeeds: number[] = [] | 69 | private downloadSpeeds: number[] = [] |
72 | 70 | ||
73 | constructor (player: videojs.Player, options: WebtorrentPluginOptions) { | 71 | constructor (player: VideoJsPlayer, options?: WebtorrentPluginOptions) { |
74 | super(player, options) | 72 | super(player) |
75 | 73 | ||
76 | this.startTime = timeToInt(options.startTime) | 74 | this.startTime = timeToInt(options.startTime) |
77 | 75 | ||
@@ -147,12 +145,12 @@ class WebTorrentPlugin extends Plugin { | |||
147 | } | 145 | } |
148 | 146 | ||
149 | // Do not display error to user because we will have multiple fallback | 147 | // Do not display error to user because we will have multiple fallback |
150 | this.disableErrorDisplay() | 148 | this.disableErrorDisplay(); |
151 | 149 | ||
152 | // Hack to "simulate" src link in video.js >= 6 | 150 | // Hack to "simulate" src link in video.js >= 6 |
153 | // Without this, we can't play the video after pausing it | 151 | // Without this, we can't play the video after pausing it |
154 | // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633 | 152 | // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633 |
155 | this.player.src = () => true | 153 | (this.player as any).src = () => true |
156 | const oldPlaybackRate = this.player.playbackRate() | 154 | const oldPlaybackRate = this.player.playbackRate() |
157 | 155 | ||
158 | const previousVideoFile = this.currentVideoFile | 156 | const previousVideoFile = this.currentVideoFile |
@@ -333,7 +331,7 @@ class WebTorrentPlugin extends Plugin { | |||
333 | 331 | ||
334 | const playPromise = this.player.play() | 332 | const playPromise = this.player.play() |
335 | if (playPromise !== undefined) { | 333 | if (playPromise !== undefined) { |
336 | return playPromise.then(done) | 334 | return playPromise.then(() => done()) |
337 | .catch((err: Error) => { | 335 | .catch((err: Error) => { |
338 | if (err.message.indexOf('The play() request was interrupted by a call to pause()') !== -1) { | 336 | if (err.message.indexOf('The play() request was interrupted by a call to pause()') !== -1) { |
339 | return | 337 | return |
@@ -426,8 +424,8 @@ class WebTorrentPlugin extends Plugin { | |||
426 | } | 424 | } |
427 | 425 | ||
428 | // Proxy first play | 426 | // Proxy first play |
429 | const oldPlay = this.player.play.bind(this.player) | 427 | const oldPlay = this.player.play.bind(this.player); |
430 | this.player.play = () => { | 428 | (this.player as any).play = () => { |
431 | this.player.addClass('vjs-has-big-play-button-clicked') | 429 | this.player.addClass('vjs-has-big-play-button-clicked') |
432 | this.player.play = oldPlay | 430 | this.player.play = oldPlay |
433 | 431 | ||
@@ -619,7 +617,7 @@ class WebTorrentPlugin extends Plugin { | |||
619 | video: qualityLevelsPayload | 617 | video: qualityLevelsPayload |
620 | } | 618 | } |
621 | } | 619 | } |
622 | this.player.tech_.trigger('loadedqualitydata', payload) | 620 | this.player.tech(true).trigger('loadedqualitydata', payload) |
623 | } | 621 | } |
624 | 622 | ||
625 | private buildQualityLabel (file: VideoFile) { | 623 | private buildQualityLabel (file: VideoFile) { |
@@ -651,9 +649,9 @@ class WebTorrentPlugin extends Plugin { | |||
651 | return | 649 | return |
652 | } | 650 | } |
653 | 651 | ||
654 | for (let i = 0; i < qualityLevels; i++) { | 652 | for (let i = 0; i < qualityLevels.length; i++) { |
655 | const q = this.player.qualityLevels[i] | 653 | const q = qualityLevels[i] |
656 | if (q.height === resolutionId) qualityLevels.selectedIndex = i | 654 | if (q.height === resolutionId) qualityLevels.selectedIndex_ = i |
657 | } | 655 | } |
658 | } | 656 | } |
659 | } | 657 | } |