]>
Commit | Line | Data |
---|---|---|
950b3ce7 | 1 | import '@peertube/videojs-contextmenu' |
57d65032 C |
2 | import './shared/upnext/end-card' |
3 | import './shared/upnext/upnext-plugin' | |
4 | import './shared/stats/stats-card' | |
5 | import './shared/stats/stats-plugin' | |
6 | import './shared/bezels/bezels-plugin' | |
7 | import './shared/peertube/peertube-plugin' | |
8 | import './shared/resolutions/peertube-resolutions-plugin' | |
9 | import './shared/control-bar/next-previous-video-button' | |
10 | import './shared/control-bar/p2p-info-button' | |
11 | import './shared/control-bar/peertube-link-button' | |
12 | import './shared/control-bar/peertube-load-progress-bar' | |
13 | import './shared/control-bar/theater-button' | |
14 | import './shared/settings/resolution-menu-button' | |
15 | import './shared/settings/resolution-menu-item' | |
16 | import './shared/settings/settings-dialog' | |
17 | import './shared/settings/settings-menu-button' | |
18 | import './shared/settings/settings-menu-item' | |
19 | import './shared/settings/settings-panel' | |
20 | import './shared/settings/settings-panel-child' | |
21 | import './shared/playlist/playlist-plugin' | |
22 | import './shared/mobile/peertube-mobile-plugin' | |
23 | import './shared/mobile/peertube-mobile-buttons' | |
24 | import './shared/hotkeys/peertube-hotkeys-plugin' | |
3e2bc4ea | 25 | import videojs from 'video.js' |
72f611ca | 26 | import { PluginsManager } from '@root-helpers/plugins-manager' |
57d65032 | 27 | import { isMobile } from '@root-helpers/web-browser' |
c4207f97 | 28 | import { saveAverageBandwidth } from './peertube-player-local-storage' |
57d65032 | 29 | import { ManagerOptionsBuilder } from './shared/manager-options' |
3f9c4955 | 30 | import { TranslationsManager } from './translations-manager' |
57d65032 | 31 | import { CommonOptions, PeertubePlayerManagerOptions, PlayerMode, PlayerNetworkInfo } from './types' |
2adfc7ea C |
32 | |
33 | // Change 'Playback Rate' to 'Speed' (smaller for our settings menu) | |
f5fcd9f7 C |
34 | (videojs.getComponent('PlaybackRateMenuButton') as any).prototype.controlText_ = 'Speed' |
35 | ||
36 | const CaptionsButton = videojs.getComponent('CaptionsButton') as any | |
2adfc7ea | 37 | // Change Captions to Subtitles/CC |
f5fcd9f7 | 38 | CaptionsButton.prototype.controlText_ = 'Subtitles/CC' |
2adfc7ea | 39 | // We just want to display 'Off' instead of 'captions off', keep a space so the variable == true (hacky I know) |
f5fcd9f7 | 40 | CaptionsButton.prototype.label_ = ' ' |
2adfc7ea | 41 | |
2adfc7ea | 42 | export class PeertubePlayerManager { |
6ec0b75b | 43 | private static playerElementClassName: string |
7e37e111 | 44 | private static onPlayerChange: (player: videojs.Player) => void |
9eccae74 | 45 | private static alreadyPlayed = false |
72f611ca | 46 | private static pluginsManager: PluginsManager |
9eccae74 | 47 | |
c4207f97 C |
48 | private static videojsDecodeErrors = 0 |
49 | ||
50 | private static p2pMediaLoaderModule: any | |
51 | ||
1a568b6f | 52 | static initState () { |
c4207f97 | 53 | this.alreadyPlayed = false |
1a568b6f C |
54 | } |
55 | ||
7e37e111 | 56 | static async initialize (mode: PlayerMode, options: PeertubePlayerManagerOptions, onPlayerChange: (player: videojs.Player) => void) { |
72f611ca | 57 | this.pluginsManager = options.pluginsManager |
58 | ||
bfbd9128 | 59 | this.onPlayerChange = onPlayerChange |
6ec0b75b C |
60 | this.playerElementClassName = options.common.playerElement.className |
61 | ||
57d65032 | 62 | if (mode === 'webtorrent') await import('./shared/webtorrent/webtorrent-plugin') |
4348a27d | 63 | if (mode === 'p2p-media-loader') { |
c4207f97 | 64 | const [ p2pMediaLoaderModule ] = await Promise.all([ |
3e254de8 | 65 | import('@peertube/p2p-media-loader-hlsjs'), |
57d65032 | 66 | import('./shared/p2p-media-loader/p2p-media-loader-plugin') |
4348a27d | 67 | ]) |
2adfc7ea | 68 | |
c4207f97 C |
69 | this.p2pMediaLoaderModule = p2pMediaLoaderModule |
70 | } | |
2adfc7ea | 71 | |
3f9c4955 | 72 | await TranslationsManager.loadLocaleInVideoJS(options.common.serverUrl, options.common.language, videojs) |
2adfc7ea | 73 | |
c4207f97 C |
74 | return this.buildPlayer(mode, options) |
75 | } | |
76 | ||
77 | private static async buildPlayer (mode: PlayerMode, options: PeertubePlayerManagerOptions): Promise<videojs.Player> { | |
9597920e | 78 | const videojsOptionsBuilder = new ManagerOptionsBuilder(mode, options, this.p2pMediaLoaderModule) |
c4207f97 C |
79 | |
80 | const videojsOptions = await this.pluginsManager.runHook( | |
81 | 'filter:internal.player.videojs.options.result', | |
82 | videojsOptionsBuilder.getVideojsOptions(this.alreadyPlayed) | |
83 | ) | |
84 | ||
2adfc7ea C |
85 | const self = this |
86 | return new Promise(res => { | |
7e37e111 | 87 | videojs(options.common.playerElement, videojsOptions, function (this: videojs.Player) { |
2adfc7ea C |
88 | const player = this |
89 | ||
536598cf C |
90 | let alreadyFallback = false |
91 | ||
c4207f97 C |
92 | const handleError = () => { |
93 | if (alreadyFallback) return | |
536598cf | 94 | alreadyFallback = true |
536598cf | 95 | |
c4207f97 C |
96 | if (mode === 'p2p-media-loader') { |
97 | self.tryToRecoverHLSError(player.error(), player, options) | |
98 | } else { | |
99 | self.maybeFallbackToWebTorrent(mode, player, options) | |
100 | } | |
101 | } | |
102 | ||
103 | player.one('error', () => handleError()) | |
6ec0b75b | 104 | |
9eccae74 | 105 | player.one('play', () => { |
c4207f97 | 106 | self.alreadyPlayed = true |
9eccae74 C |
107 | }) |
108 | ||
c4207f97 | 109 | self.addContextMenu(videojsOptionsBuilder, player, options.common) |
2adfc7ea | 110 | |
f1a0555a | 111 | if (isMobile()) player.peertubeMobile() |
d7b052ff | 112 | if (options.common.enableHotkeys === true) player.peerTubeHotkeysPlugin() |
60f013e1 | 113 | if (options.common.controlBar === false) player.controlBar.addClass('control-bar-hidden') |
f1a0555a | 114 | |
10475dea | 115 | player.bezels() |
f1a0555a | 116 | |
ff563914 RK |
117 | player.stats({ |
118 | videoUUID: options.common.videoUUID, | |
119 | videoIsLive: options.common.isLive, | |
95765067 C |
120 | mode, |
121 | p2pEnabled: options.common.p2pEnabled | |
ff563914 | 122 | }) |
10475dea | 123 | |
3e254de8 C |
124 | player.on('p2pInfo', (_, data: PlayerNetworkInfo) => { |
125 | if (data.source !== 'p2p-media-loader' || isNaN(data.bandwidthEstimate)) return | |
126 | ||
127 | saveAverageBandwidth(data.bandwidthEstimate) | |
128 | }) | |
129 | ||
2adfc7ea C |
130 | return res(player) |
131 | }) | |
132 | }) | |
133 | } | |
134 | ||
c4207f97 C |
135 | private static async tryToRecoverHLSError (err: any, currentPlayer: videojs.Player, options: PeertubePlayerManagerOptions) { |
136 | if (err.code === 3) { // Decode error | |
6ec0b75b | 137 | |
c4207f97 C |
138 | // Display a notification to user |
139 | if (this.videojsDecodeErrors === 0) { | |
140 | options.common.errorNotifier(currentPlayer.localize('The video failed to play, will try to fast forward.')) | |
2adfc7ea | 141 | } |
a950e4c8 | 142 | |
c4207f97 C |
143 | if (this.videojsDecodeErrors === 20) { |
144 | this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options) | |
145 | return | |
2adfc7ea | 146 | } |
39aad8cc | 147 | |
c4207f97 | 148 | console.log('Fast forwarding HLS to recover from an error.') |
dca0fe12 | 149 | |
c4207f97 | 150 | this.videojsDecodeErrors++ |
39aad8cc | 151 | |
c4207f97 C |
152 | options.common.startTime = currentPlayer.currentTime() + 2 |
153 | options.common.autoplay = true | |
154 | this.rebuildAndUpdateVideoElement(currentPlayer, options.common) | |
39aad8cc | 155 | |
c4207f97 C |
156 | const newPlayer = await this.buildPlayer('p2p-media-loader', options) |
157 | this.onPlayerChange(newPlayer) | |
158 | } else { | |
159 | this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options) | |
2adfc7ea | 160 | } |
39aad8cc C |
161 | } |
162 | ||
c4207f97 C |
163 | private static async maybeFallbackToWebTorrent ( |
164 | currentMode: PlayerMode, | |
165 | currentPlayer: videojs.Player, | |
166 | options: PeertubePlayerManagerOptions | |
167 | ) { | |
168 | if (options.webtorrent.videoFiles.length === 0 || currentMode === 'webtorrent') { | |
169 | currentPlayer.peertube().displayFatalError() | |
170 | return | |
1dc240a9 RK |
171 | } |
172 | ||
c4207f97 | 173 | console.log('Fallback to webtorrent.') |
2adfc7ea | 174 | |
c4207f97 | 175 | this.rebuildAndUpdateVideoElement(currentPlayer, options.common) |
2adfc7ea | 176 | |
57d65032 | 177 | await import('./shared/webtorrent/webtorrent-plugin') |
2adfc7ea | 178 | |
c4207f97 C |
179 | const newPlayer = await this.buildPlayer('webtorrent', options) |
180 | this.onPlayerChange(newPlayer) | |
2adfc7ea C |
181 | } |
182 | ||
c4207f97 C |
183 | private static rebuildAndUpdateVideoElement (player: videojs.Player, commonOptions: CommonOptions) { |
184 | const newVideoElement = document.createElement('video') | |
185 | newVideoElement.className = this.playerElementClassName | |
9162fdd3 | 186 | |
c4207f97 C |
187 | // VideoJS wraps our video element inside a div |
188 | let currentParentPlayerElement = commonOptions.playerElement.parentNode | |
189 | // Fix on IOS, don't ask me why | |
190 | if (!currentParentPlayerElement) currentParentPlayerElement = document.getElementById(commonOptions.playerElement.id).parentNode | |
a472cf03 | 191 | |
c4207f97 | 192 | currentParentPlayerElement.parentNode.insertBefore(newVideoElement, currentParentPlayerElement) |
2adfc7ea | 193 | |
c4207f97 C |
194 | commonOptions.playerElement = newVideoElement |
195 | commonOptions.onPlayerElementChange(newVideoElement) | |
ff563914 | 196 | |
c4207f97 | 197 | player.dispose() |
2adfc7ea | 198 | |
c4207f97 | 199 | return newVideoElement |
2adfc7ea C |
200 | } |
201 | ||
9597920e | 202 | private static addContextMenu (optionsBuilder: ManagerOptionsBuilder, player: videojs.Player, commonOptions: CommonOptions) { |
c4207f97 | 203 | const options = optionsBuilder.getContextMenuOptions(player, commonOptions) |
64228474 | 204 | |
c4207f97 | 205 | player.contextmenuUI(options) |
64228474 | 206 | } |
2adfc7ea C |
207 | } |
208 | ||
209 | // ############################################################################ | |
210 | ||
211 | export { | |
212 | videojs | |
213 | } |