]>
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' | |
fd3c2e87 | 25 | import './shared/metrics/metrics-plugin' |
3e2bc4ea | 26 | import videojs from 'video.js' |
42b40636 | 27 | import { logger } from '@root-helpers/logger' |
72f611ca | 28 | import { PluginsManager } from '@root-helpers/plugins-manager' |
57d65032 | 29 | import { isMobile } from '@root-helpers/web-browser' |
c4207f97 | 30 | import { saveAverageBandwidth } from './peertube-player-local-storage' |
57d65032 | 31 | import { ManagerOptionsBuilder } from './shared/manager-options' |
3f9c4955 | 32 | import { TranslationsManager } from './translations-manager' |
57d65032 | 33 | import { CommonOptions, PeertubePlayerManagerOptions, PlayerMode, PlayerNetworkInfo } from './types' |
2adfc7ea C |
34 | |
35 | // Change 'Playback Rate' to 'Speed' (smaller for our settings menu) | |
f5fcd9f7 C |
36 | (videojs.getComponent('PlaybackRateMenuButton') as any).prototype.controlText_ = 'Speed' |
37 | ||
38 | const CaptionsButton = videojs.getComponent('CaptionsButton') as any | |
2adfc7ea | 39 | // Change Captions to Subtitles/CC |
f5fcd9f7 | 40 | CaptionsButton.prototype.controlText_ = 'Subtitles/CC' |
2adfc7ea | 41 | // We just want to display 'Off' instead of 'captions off', keep a space so the variable == true (hacky I know) |
f5fcd9f7 | 42 | CaptionsButton.prototype.label_ = ' ' |
2adfc7ea | 43 | |
2adfc7ea | 44 | export class PeertubePlayerManager { |
6ec0b75b | 45 | private static playerElementClassName: string |
eaa5dc31 C |
46 | private static playerElementAttributes: { name: string, value: string }[] = [] |
47 | ||
7e37e111 | 48 | private static onPlayerChange: (player: videojs.Player) => void |
9eccae74 | 49 | private static alreadyPlayed = false |
72f611ca | 50 | private static pluginsManager: PluginsManager |
9eccae74 | 51 | |
c4207f97 C |
52 | private static videojsDecodeErrors = 0 |
53 | ||
54 | private static p2pMediaLoaderModule: any | |
55 | ||
1a568b6f | 56 | static initState () { |
c4207f97 | 57 | this.alreadyPlayed = false |
1a568b6f C |
58 | } |
59 | ||
7e37e111 | 60 | static async initialize (mode: PlayerMode, options: PeertubePlayerManagerOptions, onPlayerChange: (player: videojs.Player) => void) { |
72f611ca | 61 | this.pluginsManager = options.pluginsManager |
62 | ||
bfbd9128 | 63 | this.onPlayerChange = onPlayerChange |
eaa5dc31 | 64 | |
6ec0b75b C |
65 | this.playerElementClassName = options.common.playerElement.className |
66 | ||
eaa5dc31 C |
67 | for (const name of options.common.playerElement.getAttributeNames()) { |
68 | this.playerElementAttributes.push({ name, value: options.common.playerElement.getAttribute(name) }) | |
69 | } | |
70 | ||
57d65032 | 71 | if (mode === 'webtorrent') await import('./shared/webtorrent/webtorrent-plugin') |
4348a27d | 72 | if (mode === 'p2p-media-loader') { |
c4207f97 | 73 | const [ p2pMediaLoaderModule ] = await Promise.all([ |
3e254de8 | 74 | import('@peertube/p2p-media-loader-hlsjs'), |
57d65032 | 75 | import('./shared/p2p-media-loader/p2p-media-loader-plugin') |
4348a27d | 76 | ]) |
2adfc7ea | 77 | |
c4207f97 C |
78 | this.p2pMediaLoaderModule = p2pMediaLoaderModule |
79 | } | |
2adfc7ea | 80 | |
3f9c4955 | 81 | await TranslationsManager.loadLocaleInVideoJS(options.common.serverUrl, options.common.language, videojs) |
2adfc7ea | 82 | |
c4207f97 C |
83 | return this.buildPlayer(mode, options) |
84 | } | |
85 | ||
86 | private static async buildPlayer (mode: PlayerMode, options: PeertubePlayerManagerOptions): Promise<videojs.Player> { | |
9597920e | 87 | const videojsOptionsBuilder = new ManagerOptionsBuilder(mode, options, this.p2pMediaLoaderModule) |
c4207f97 C |
88 | |
89 | const videojsOptions = await this.pluginsManager.runHook( | |
90 | 'filter:internal.player.videojs.options.result', | |
91 | videojsOptionsBuilder.getVideojsOptions(this.alreadyPlayed) | |
92 | ) | |
93 | ||
2adfc7ea C |
94 | const self = this |
95 | return new Promise(res => { | |
7e37e111 | 96 | videojs(options.common.playerElement, videojsOptions, function (this: videojs.Player) { |
2adfc7ea C |
97 | const player = this |
98 | ||
536598cf C |
99 | let alreadyFallback = false |
100 | ||
c4207f97 C |
101 | const handleError = () => { |
102 | if (alreadyFallback) return | |
536598cf | 103 | alreadyFallback = true |
536598cf | 104 | |
c4207f97 C |
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()) | |
6ec0b75b | 113 | |
9eccae74 | 114 | player.one('play', () => { |
c4207f97 | 115 | self.alreadyPlayed = true |
9eccae74 C |
116 | }) |
117 | ||
c4207f97 | 118 | self.addContextMenu(videojsOptionsBuilder, player, options.common) |
2adfc7ea | 119 | |
f1a0555a | 120 | if (isMobile()) player.peertubeMobile() |
d7b052ff | 121 | if (options.common.enableHotkeys === true) player.peerTubeHotkeysPlugin() |
60f013e1 | 122 | if (options.common.controlBar === false) player.controlBar.addClass('control-bar-hidden') |
f1a0555a | 123 | |
10475dea | 124 | player.bezels() |
f1a0555a | 125 | |
ff563914 RK |
126 | player.stats({ |
127 | videoUUID: options.common.videoUUID, | |
128 | videoIsLive: options.common.isLive, | |
95765067 C |
129 | mode, |
130 | p2pEnabled: options.common.p2pEnabled | |
ff563914 | 131 | }) |
10475dea | 132 | |
3e254de8 C |
133 | player.on('p2pInfo', (_, data: PlayerNetworkInfo) => { |
134 | if (data.source !== 'p2p-media-loader' || isNaN(data.bandwidthEstimate)) return | |
135 | ||
136 | saveAverageBandwidth(data.bandwidthEstimate) | |
137 | }) | |
138 | ||
f2a16d93 | 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 | ||
3f9decbd C |
143 | let offlineNotificationElemAdded = false |
144 | ||
f2a16d93 | 145 | const handleOnline = () => { |
3f9decbd C |
146 | if (!offlineNotificationElemAdded) return |
147 | ||
f2a16d93 | 148 | player.el().removeChild(offlineNotificationElem) |
3f9decbd C |
149 | offlineNotificationElemAdded = false |
150 | ||
f2a16d93 | 151 | logger.info('The browser is online') |
152 | } | |
153 | ||
154 | const handleOffline = () => { | |
3f9decbd C |
155 | if (offlineNotificationElemAdded) return |
156 | ||
f2a16d93 | 157 | player.el().appendChild(offlineNotificationElem) |
3f9decbd C |
158 | offlineNotificationElemAdded = true |
159 | ||
f2a16d93 | 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 | ||
2adfc7ea C |
171 | return res(player) |
172 | }) | |
173 | }) | |
174 | } | |
175 | ||
c4207f97 | 176 | private static async tryToRecoverHLSError (err: any, currentPlayer: videojs.Player, options: PeertubePlayerManagerOptions) { |
f746622b | 177 | if (err.code === MediaError.MEDIA_ERR_DECODE) { |
6ec0b75b | 178 | |
c4207f97 C |
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.')) | |
2adfc7ea | 182 | } |
a950e4c8 | 183 | |
c4207f97 C |
184 | if (this.videojsDecodeErrors === 20) { |
185 | this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options) | |
186 | return | |
2adfc7ea | 187 | } |
39aad8cc | 188 | |
42b40636 | 189 | logger.info('Fast forwarding HLS to recover from an error.') |
dca0fe12 | 190 | |
c4207f97 | 191 | this.videojsDecodeErrors++ |
39aad8cc | 192 | |
c4207f97 C |
193 | options.common.startTime = currentPlayer.currentTime() + 2 |
194 | options.common.autoplay = true | |
195 | this.rebuildAndUpdateVideoElement(currentPlayer, options.common) | |
39aad8cc | 196 | |
c4207f97 C |
197 | const newPlayer = await this.buildPlayer('p2p-media-loader', options) |
198 | this.onPlayerChange(newPlayer) | |
199 | } else { | |
200 | this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options) | |
2adfc7ea | 201 | } |
39aad8cc C |
202 | } |
203 | ||
c4207f97 C |
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 | |
1dc240a9 RK |
212 | } |
213 | ||
42b40636 | 214 | logger.info('Fallback to webtorrent.') |
2adfc7ea | 215 | |
c4207f97 | 216 | this.rebuildAndUpdateVideoElement(currentPlayer, options.common) |
2adfc7ea | 217 | |
57d65032 | 218 | await import('./shared/webtorrent/webtorrent-plugin') |
2adfc7ea | 219 | |
c4207f97 C |
220 | const newPlayer = await this.buildPlayer('webtorrent', options) |
221 | this.onPlayerChange(newPlayer) | |
2adfc7ea C |
222 | } |
223 | ||
c4207f97 C |
224 | private static rebuildAndUpdateVideoElement (player: videojs.Player, commonOptions: CommonOptions) { |
225 | const newVideoElement = document.createElement('video') | |
eaa5dc31 C |
226 | |
227 | // Reset class | |
c4207f97 | 228 | newVideoElement.className = this.playerElementClassName |
9162fdd3 | 229 | |
eaa5dc31 C |
230 | // Reapply attributes |
231 | for (const { name, value } of this.playerElementAttributes) { | |
232 | newVideoElement.setAttribute(name, value) | |
233 | } | |
234 | ||
c4207f97 C |
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 | |
a472cf03 | 239 | |
c4207f97 | 240 | currentParentPlayerElement.parentNode.insertBefore(newVideoElement, currentParentPlayerElement) |
2adfc7ea | 241 | |
c4207f97 C |
242 | commonOptions.playerElement = newVideoElement |
243 | commonOptions.onPlayerElementChange(newVideoElement) | |
ff563914 | 244 | |
c4207f97 | 245 | player.dispose() |
2adfc7ea | 246 | |
c4207f97 | 247 | return newVideoElement |
2adfc7ea C |
248 | } |
249 | ||
9597920e | 250 | private static addContextMenu (optionsBuilder: ManagerOptionsBuilder, player: videojs.Player, commonOptions: CommonOptions) { |
c4207f97 | 251 | const options = optionsBuilder.getContextMenuOptions(player, commonOptions) |
64228474 | 252 | |
c4207f97 | 253 | player.contextmenuUI(options) |
64228474 | 254 | } |
2adfc7ea C |
255 | } |
256 | ||
257 | // ############################################################################ | |
258 | ||
259 | export { | |
260 | videojs | |
261 | } |