]>
Commit | Line | Data |
---|---|---|
1 | import '@peertube/videojs-contextmenu' | |
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' | |
25 | import './shared/metrics/metrics-plugin' | |
26 | import videojs from 'video.js' | |
27 | import { logger } from '@root-helpers/logger' | |
28 | import { PluginsManager } from '@root-helpers/plugins-manager' | |
29 | import { isMobile } from '@root-helpers/web-browser' | |
30 | import { saveAverageBandwidth } from './peertube-player-local-storage' | |
31 | import { ManagerOptionsBuilder } from './shared/manager-options' | |
32 | import { TranslationsManager } from './translations-manager' | |
33 | import { CommonOptions, PeertubePlayerManagerOptions, PlayerMode, PlayerNetworkInfo } from './types' | |
34 | ||
35 | // Change 'Playback Rate' to 'Speed' (smaller for our settings menu) | |
36 | (videojs.getComponent('PlaybackRateMenuButton') as any).prototype.controlText_ = 'Speed' | |
37 | ||
38 | const CaptionsButton = videojs.getComponent('CaptionsButton') as any | |
39 | // Change Captions to Subtitles/CC | |
40 | CaptionsButton.prototype.controlText_ = 'Subtitles/CC' | |
41 | // We just want to display 'Off' instead of 'captions off', keep a space so the variable == true (hacky I know) | |
42 | CaptionsButton.prototype.label_ = ' ' | |
43 | ||
44 | export class PeertubePlayerManager { | |
45 | private static playerElementClassName: string | |
46 | private static playerElementAttributes: { name: string, value: string }[] = [] | |
47 | ||
48 | private static onPlayerChange: (player: videojs.Player) => void | |
49 | private static alreadyPlayed = false | |
50 | private static pluginsManager: PluginsManager | |
51 | ||
52 | private static videojsDecodeErrors = 0 | |
53 | ||
54 | private static p2pMediaLoaderModule: any | |
55 | ||
56 | static initState () { | |
57 | this.alreadyPlayed = false | |
58 | } | |
59 | ||
60 | static async initialize (mode: PlayerMode, options: PeertubePlayerManagerOptions, onPlayerChange: (player: videojs.Player) => void) { | |
61 | this.pluginsManager = options.pluginsManager | |
62 | ||
63 | this.onPlayerChange = onPlayerChange | |
64 | ||
65 | this.playerElementClassName = options.common.playerElement.className | |
66 | ||
67 | for (const name of options.common.playerElement.getAttributeNames()) { | |
68 | this.playerElementAttributes.push({ name, value: options.common.playerElement.getAttribute(name) }) | |
69 | } | |
70 | ||
71 | if (mode === 'webtorrent') await import('./shared/webtorrent/webtorrent-plugin') | |
72 | if (mode === 'p2p-media-loader') { | |
73 | const [ p2pMediaLoaderModule ] = await Promise.all([ | |
74 | import('@peertube/p2p-media-loader-hlsjs'), | |
75 | import('./shared/p2p-media-loader/p2p-media-loader-plugin') | |
76 | ]) | |
77 | ||
78 | this.p2pMediaLoaderModule = p2pMediaLoaderModule | |
79 | } | |
80 | ||
81 | await TranslationsManager.loadLocaleInVideoJS(options.common.serverUrl, options.common.language, videojs) | |
82 | ||
83 | return this.buildPlayer(mode, options) | |
84 | } | |
85 | ||
86 | private static async buildPlayer (mode: PlayerMode, options: PeertubePlayerManagerOptions): Promise<videojs.Player> { | |
87 | const videojsOptionsBuilder = new ManagerOptionsBuilder(mode, options, this.p2pMediaLoaderModule) | |
88 | ||
89 | const videojsOptions = await this.pluginsManager.runHook( | |
90 | 'filter:internal.player.videojs.options.result', | |
91 | videojsOptionsBuilder.getVideojsOptions(this.alreadyPlayed) | |
92 | ) | |
93 | ||
94 | const self = this | |
95 | return new Promise(res => { | |
96 | videojs(options.common.playerElement, videojsOptions, function (this: videojs.Player) { | |
97 | const player = this | |
98 | ||
99 | let alreadyFallback = false | |
100 | ||
101 | const handleError = () => { | |
102 | if (alreadyFallback) return | |
103 | alreadyFallback = true | |
104 | ||
105 | if (mode === 'p2p-media-loader') { | |
106 | self.tryToRecoverHLSError(player.error(), player, options) | |
107 | } else { | |
108 | self.maybeFallbackToWebTorrent(mode, player, options) | |
109 | } | |
110 | } | |
111 | ||
112 | player.one('error', () => handleError()) | |
113 | ||
114 | player.one('play', () => { | |
115 | self.alreadyPlayed = true | |
116 | }) | |
117 | ||
118 | self.addContextMenu(videojsOptionsBuilder, player, options.common) | |
119 | ||
120 | if (isMobile()) player.peertubeMobile() | |
121 | if (options.common.enableHotkeys === true) player.peerTubeHotkeysPlugin() | |
122 | if (options.common.controlBar === false) player.controlBar.addClass('control-bar-hidden') | |
123 | ||
124 | player.bezels() | |
125 | ||
126 | player.stats({ | |
127 | videoUUID: options.common.videoUUID, | |
128 | videoIsLive: options.common.isLive, | |
129 | mode, | |
130 | p2pEnabled: options.common.p2pEnabled | |
131 | }) | |
132 | ||
133 | player.on('p2pInfo', (_, data: PlayerNetworkInfo) => { | |
134 | if (data.source !== 'p2p-media-loader' || isNaN(data.bandwidthEstimate)) return | |
135 | ||
136 | saveAverageBandwidth(data.bandwidthEstimate) | |
137 | }) | |
138 | ||
139 | const offlineNotificationElem = document.createElement('div') | |
140 | offlineNotificationElem.classList.add('vjs-peertube-offline-notification') | |
141 | offlineNotificationElem.innerText = player.localize('You seem to be offline and the video may not work') | |
142 | ||
143 | let offlineNotificationElemAdded = false | |
144 | ||
145 | const handleOnline = () => { | |
146 | if (!offlineNotificationElemAdded) return | |
147 | ||
148 | player.el().removeChild(offlineNotificationElem) | |
149 | offlineNotificationElemAdded = false | |
150 | ||
151 | logger.info('The browser is online') | |
152 | } | |
153 | ||
154 | const handleOffline = () => { | |
155 | if (offlineNotificationElemAdded) return | |
156 | ||
157 | player.el().appendChild(offlineNotificationElem) | |
158 | offlineNotificationElemAdded = true | |
159 | ||
160 | logger.info('The browser is offline') | |
161 | } | |
162 | ||
163 | window.addEventListener('online', handleOnline) | |
164 | window.addEventListener('offline', handleOffline) | |
165 | ||
166 | player.on('dispose', () => { | |
167 | window.removeEventListener('online', handleOnline) | |
168 | window.removeEventListener('offline', handleOffline) | |
169 | }) | |
170 | ||
171 | return res(player) | |
172 | }) | |
173 | }) | |
174 | } | |
175 | ||
176 | private static async tryToRecoverHLSError (err: any, currentPlayer: videojs.Player, options: PeertubePlayerManagerOptions) { | |
177 | if (err.code === MediaError.MEDIA_ERR_DECODE) { | |
178 | ||
179 | // Display a notification to user | |
180 | if (this.videojsDecodeErrors === 0) { | |
181 | options.common.errorNotifier(currentPlayer.localize('The video failed to play, will try to fast forward.')) | |
182 | } | |
183 | ||
184 | if (this.videojsDecodeErrors === 20) { | |
185 | this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options) | |
186 | return | |
187 | } | |
188 | ||
189 | logger.info('Fast forwarding HLS to recover from an error.') | |
190 | ||
191 | this.videojsDecodeErrors++ | |
192 | ||
193 | options.common.startTime = currentPlayer.currentTime() + 2 | |
194 | options.common.autoplay = true | |
195 | this.rebuildAndUpdateVideoElement(currentPlayer, options.common) | |
196 | ||
197 | const newPlayer = await this.buildPlayer('p2p-media-loader', options) | |
198 | this.onPlayerChange(newPlayer) | |
199 | } else { | |
200 | this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options) | |
201 | } | |
202 | } | |
203 | ||
204 | private static async maybeFallbackToWebTorrent ( | |
205 | currentMode: PlayerMode, | |
206 | currentPlayer: videojs.Player, | |
207 | options: PeertubePlayerManagerOptions | |
208 | ) { | |
209 | if (options.webtorrent.videoFiles.length === 0 || currentMode === 'webtorrent') { | |
210 | currentPlayer.peertube().displayFatalError() | |
211 | return | |
212 | } | |
213 | ||
214 | logger.info('Fallback to webtorrent.') | |
215 | ||
216 | this.rebuildAndUpdateVideoElement(currentPlayer, options.common) | |
217 | ||
218 | await import('./shared/webtorrent/webtorrent-plugin') | |
219 | ||
220 | const newPlayer = await this.buildPlayer('webtorrent', options) | |
221 | this.onPlayerChange(newPlayer) | |
222 | } | |
223 | ||
224 | private static rebuildAndUpdateVideoElement (player: videojs.Player, commonOptions: CommonOptions) { | |
225 | const newVideoElement = document.createElement('video') | |
226 | ||
227 | // Reset class | |
228 | newVideoElement.className = this.playerElementClassName | |
229 | ||
230 | // Reapply attributes | |
231 | for (const { name, value } of this.playerElementAttributes) { | |
232 | newVideoElement.setAttribute(name, value) | |
233 | } | |
234 | ||
235 | // VideoJS wraps our video element inside a div | |
236 | let currentParentPlayerElement = commonOptions.playerElement.parentNode | |
237 | // Fix on IOS, don't ask me why | |
238 | if (!currentParentPlayerElement) currentParentPlayerElement = document.getElementById(commonOptions.playerElement.id).parentNode | |
239 | ||
240 | currentParentPlayerElement.parentNode.insertBefore(newVideoElement, currentParentPlayerElement) | |
241 | ||
242 | commonOptions.playerElement = newVideoElement | |
243 | commonOptions.onPlayerElementChange(newVideoElement) | |
244 | ||
245 | player.dispose() | |
246 | ||
247 | return newVideoElement | |
248 | } | |
249 | ||
250 | private static addContextMenu (optionsBuilder: ManagerOptionsBuilder, player: videojs.Player, commonOptions: CommonOptions) { | |
251 | const options = optionsBuilder.getContextMenuOptions(player, commonOptions) | |
252 | ||
253 | player.contextmenuUI(options) | |
254 | } | |
255 | } | |
256 | ||
257 | // ############################################################################ | |
258 | ||
259 | export { | |
260 | videojs | |
261 | } |