]>
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 |
7e37e111 | 46 | private static onPlayerChange: (player: videojs.Player) => void |
9eccae74 | 47 | private static alreadyPlayed = false |
72f611ca | 48 | private static pluginsManager: PluginsManager |
9eccae74 | 49 | |
c4207f97 C |
50 | private static videojsDecodeErrors = 0 |
51 | ||
52 | private static p2pMediaLoaderModule: any | |
53 | ||
1a568b6f | 54 | static initState () { |
c4207f97 | 55 | this.alreadyPlayed = false |
1a568b6f C |
56 | } |
57 | ||
7e37e111 | 58 | static async initialize (mode: PlayerMode, options: PeertubePlayerManagerOptions, onPlayerChange: (player: videojs.Player) => void) { |
72f611ca | 59 | this.pluginsManager = options.pluginsManager |
60 | ||
bfbd9128 | 61 | this.onPlayerChange = onPlayerChange |
6ec0b75b C |
62 | this.playerElementClassName = options.common.playerElement.className |
63 | ||
57d65032 | 64 | if (mode === 'webtorrent') await import('./shared/webtorrent/webtorrent-plugin') |
4348a27d | 65 | if (mode === 'p2p-media-loader') { |
c4207f97 | 66 | const [ p2pMediaLoaderModule ] = await Promise.all([ |
3e254de8 | 67 | import('@peertube/p2p-media-loader-hlsjs'), |
57d65032 | 68 | import('./shared/p2p-media-loader/p2p-media-loader-plugin') |
4348a27d | 69 | ]) |
2adfc7ea | 70 | |
c4207f97 C |
71 | this.p2pMediaLoaderModule = p2pMediaLoaderModule |
72 | } | |
2adfc7ea | 73 | |
3f9c4955 | 74 | await TranslationsManager.loadLocaleInVideoJS(options.common.serverUrl, options.common.language, videojs) |
2adfc7ea | 75 | |
c4207f97 C |
76 | return this.buildPlayer(mode, options) |
77 | } | |
78 | ||
79 | private static async buildPlayer (mode: PlayerMode, options: PeertubePlayerManagerOptions): Promise<videojs.Player> { | |
9597920e | 80 | const videojsOptionsBuilder = new ManagerOptionsBuilder(mode, options, this.p2pMediaLoaderModule) |
c4207f97 C |
81 | |
82 | const videojsOptions = await this.pluginsManager.runHook( | |
83 | 'filter:internal.player.videojs.options.result', | |
84 | videojsOptionsBuilder.getVideojsOptions(this.alreadyPlayed) | |
85 | ) | |
86 | ||
2adfc7ea C |
87 | const self = this |
88 | return new Promise(res => { | |
7e37e111 | 89 | videojs(options.common.playerElement, videojsOptions, function (this: videojs.Player) { |
2adfc7ea C |
90 | const player = this |
91 | ||
536598cf C |
92 | let alreadyFallback = false |
93 | ||
c4207f97 C |
94 | const handleError = () => { |
95 | if (alreadyFallback) return | |
536598cf | 96 | alreadyFallback = true |
536598cf | 97 | |
c4207f97 C |
98 | if (mode === 'p2p-media-loader') { |
99 | self.tryToRecoverHLSError(player.error(), player, options) | |
100 | } else { | |
101 | self.maybeFallbackToWebTorrent(mode, player, options) | |
102 | } | |
103 | } | |
104 | ||
105 | player.one('error', () => handleError()) | |
6ec0b75b | 106 | |
9eccae74 | 107 | player.one('play', () => { |
c4207f97 | 108 | self.alreadyPlayed = true |
9eccae74 C |
109 | }) |
110 | ||
c4207f97 | 111 | self.addContextMenu(videojsOptionsBuilder, player, options.common) |
2adfc7ea | 112 | |
f1a0555a | 113 | if (isMobile()) player.peertubeMobile() |
d7b052ff | 114 | if (options.common.enableHotkeys === true) player.peerTubeHotkeysPlugin() |
60f013e1 | 115 | if (options.common.controlBar === false) player.controlBar.addClass('control-bar-hidden') |
f1a0555a | 116 | |
10475dea | 117 | player.bezels() |
f1a0555a | 118 | |
ff563914 RK |
119 | player.stats({ |
120 | videoUUID: options.common.videoUUID, | |
121 | videoIsLive: options.common.isLive, | |
95765067 C |
122 | mode, |
123 | p2pEnabled: options.common.p2pEnabled | |
ff563914 | 124 | }) |
10475dea | 125 | |
3e254de8 C |
126 | player.on('p2pInfo', (_, data: PlayerNetworkInfo) => { |
127 | if (data.source !== 'p2p-media-loader' || isNaN(data.bandwidthEstimate)) return | |
128 | ||
129 | saveAverageBandwidth(data.bandwidthEstimate) | |
130 | }) | |
131 | ||
f2a16d93 | 132 | const offlineNotificationElem = document.createElement('div') |
133 | offlineNotificationElem.classList.add('vjs-peertube-offline-notification') | |
134 | offlineNotificationElem.innerText = player.localize('You seem to be offline and the video may not work') | |
135 | ||
136 | const handleOnline = () => { | |
137 | player.el().removeChild(offlineNotificationElem) | |
138 | logger.info('The browser is online') | |
139 | } | |
140 | ||
141 | const handleOffline = () => { | |
142 | player.el().appendChild(offlineNotificationElem) | |
143 | logger.info('The browser is offline') | |
144 | } | |
145 | ||
146 | window.addEventListener('online', handleOnline) | |
147 | window.addEventListener('offline', handleOffline) | |
148 | ||
149 | player.on('dispose', () => { | |
150 | window.removeEventListener('online', handleOnline) | |
151 | window.removeEventListener('offline', handleOffline) | |
152 | }) | |
153 | ||
2adfc7ea C |
154 | return res(player) |
155 | }) | |
156 | }) | |
157 | } | |
158 | ||
c4207f97 C |
159 | private static async tryToRecoverHLSError (err: any, currentPlayer: videojs.Player, options: PeertubePlayerManagerOptions) { |
160 | if (err.code === 3) { // Decode error | |
6ec0b75b | 161 | |
c4207f97 C |
162 | // Display a notification to user |
163 | if (this.videojsDecodeErrors === 0) { | |
164 | options.common.errorNotifier(currentPlayer.localize('The video failed to play, will try to fast forward.')) | |
2adfc7ea | 165 | } |
a950e4c8 | 166 | |
c4207f97 C |
167 | if (this.videojsDecodeErrors === 20) { |
168 | this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options) | |
169 | return | |
2adfc7ea | 170 | } |
39aad8cc | 171 | |
42b40636 | 172 | logger.info('Fast forwarding HLS to recover from an error.') |
dca0fe12 | 173 | |
c4207f97 | 174 | this.videojsDecodeErrors++ |
39aad8cc | 175 | |
c4207f97 C |
176 | options.common.startTime = currentPlayer.currentTime() + 2 |
177 | options.common.autoplay = true | |
178 | this.rebuildAndUpdateVideoElement(currentPlayer, options.common) | |
39aad8cc | 179 | |
c4207f97 C |
180 | const newPlayer = await this.buildPlayer('p2p-media-loader', options) |
181 | this.onPlayerChange(newPlayer) | |
182 | } else { | |
183 | this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options) | |
2adfc7ea | 184 | } |
39aad8cc C |
185 | } |
186 | ||
c4207f97 C |
187 | private static async maybeFallbackToWebTorrent ( |
188 | currentMode: PlayerMode, | |
189 | currentPlayer: videojs.Player, | |
190 | options: PeertubePlayerManagerOptions | |
191 | ) { | |
192 | if (options.webtorrent.videoFiles.length === 0 || currentMode === 'webtorrent') { | |
193 | currentPlayer.peertube().displayFatalError() | |
194 | return | |
1dc240a9 RK |
195 | } |
196 | ||
42b40636 | 197 | logger.info('Fallback to webtorrent.') |
2adfc7ea | 198 | |
c4207f97 | 199 | this.rebuildAndUpdateVideoElement(currentPlayer, options.common) |
2adfc7ea | 200 | |
57d65032 | 201 | await import('./shared/webtorrent/webtorrent-plugin') |
2adfc7ea | 202 | |
c4207f97 C |
203 | const newPlayer = await this.buildPlayer('webtorrent', options) |
204 | this.onPlayerChange(newPlayer) | |
2adfc7ea C |
205 | } |
206 | ||
c4207f97 C |
207 | private static rebuildAndUpdateVideoElement (player: videojs.Player, commonOptions: CommonOptions) { |
208 | const newVideoElement = document.createElement('video') | |
209 | newVideoElement.className = this.playerElementClassName | |
9162fdd3 | 210 | |
c4207f97 C |
211 | // VideoJS wraps our video element inside a div |
212 | let currentParentPlayerElement = commonOptions.playerElement.parentNode | |
213 | // Fix on IOS, don't ask me why | |
214 | if (!currentParentPlayerElement) currentParentPlayerElement = document.getElementById(commonOptions.playerElement.id).parentNode | |
a472cf03 | 215 | |
c4207f97 | 216 | currentParentPlayerElement.parentNode.insertBefore(newVideoElement, currentParentPlayerElement) |
2adfc7ea | 217 | |
c4207f97 C |
218 | commonOptions.playerElement = newVideoElement |
219 | commonOptions.onPlayerElementChange(newVideoElement) | |
ff563914 | 220 | |
c4207f97 | 221 | player.dispose() |
2adfc7ea | 222 | |
c4207f97 | 223 | return newVideoElement |
2adfc7ea C |
224 | } |
225 | ||
9597920e | 226 | private static addContextMenu (optionsBuilder: ManagerOptionsBuilder, player: videojs.Player, commonOptions: CommonOptions) { |
c4207f97 | 227 | const options = optionsBuilder.getContextMenuOptions(player, commonOptions) |
64228474 | 228 | |
c4207f97 | 229 | player.contextmenuUI(options) |
64228474 | 230 | } |
2adfc7ea C |
231 | } |
232 | ||
233 | // ############################################################################ | |
234 | ||
235 | export { | |
236 | videojs | |
237 | } |