]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-player-manager.ts
Display avatar in embed poster
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-player-manager.ts
CommitLineData
950b3ce7 1import '@peertube/videojs-contextmenu'
f5fcd9f7 2import './upnext/end-card'
3bcb4fd7 3import './upnext/upnext-plugin'
ff563914
RK
4import './stats/stats-card'
5import './stats/stats-plugin'
62ab565d 6import './bezels/bezels-plugin'
2adfc7ea 7import './peertube-plugin'
e367da94 8import './peertube-resolutions-plugin'
a950e4c8 9import './videojs-components/next-previous-video-button'
f5fcd9f7 10import './videojs-components/p2p-info-button'
2adfc7ea 11import './videojs-components/peertube-link-button'
f5fcd9f7 12import './videojs-components/peertube-load-progress-bar'
2adfc7ea 13import './videojs-components/resolution-menu-button'
f5fcd9f7
C
14import './videojs-components/resolution-menu-item'
15import './videojs-components/settings-dialog'
2adfc7ea 16import './videojs-components/settings-menu-button'
f5fcd9f7
C
17import './videojs-components/settings-menu-item'
18import './videojs-components/settings-panel'
19import './videojs-components/settings-panel-child'
2adfc7ea 20import './videojs-components/theater-button'
4572c3d0 21import './playlist/playlist-plugin'
f1a0555a 22import './mobile/peertube-mobile-plugin'
2dd0a8a8 23import './mobile/peertube-mobile-buttons'
d7b052ff 24import './hotkeys/peertube-hotkeys-plugin'
3e2bc4ea 25import videojs from 'video.js'
72f611ca 26import { PluginsManager } from '@root-helpers/plugins-manager'
c4207f97
C
27import { saveAverageBandwidth } from './peertube-player-local-storage'
28import { CommonOptions, PeertubePlayerManagerOptions, PeertubePlayerOptionsBuilder, PlayerMode } from './peertube-player-options-builder'
29import { PlayerNetworkInfo } from './peertube-videojs-typings'
3f9c4955 30import { TranslationsManager } from './translations-manager'
c4207f97 31import { isMobile } from './utils'
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
36const CaptionsButton = videojs.getComponent('CaptionsButton') as any
2adfc7ea 37// Change Captions to Subtitles/CC
f5fcd9f7 38CaptionsButton.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 40CaptionsButton.prototype.label_ = ' '
2adfc7ea 41
2adfc7ea 42export 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
09209296 62 if (mode === 'webtorrent') await import('./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'),
09209296 66 import('./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> {
78 const videojsOptionsBuilder = new PeertubePlayerOptionsBuilder(mode, options, this.p2pMediaLoaderModule)
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()
f1a0555a 113
10475dea 114 player.bezels()
f1a0555a 115
ff563914
RK
116 player.stats({
117 videoUUID: options.common.videoUUID,
118 videoIsLive: options.common.isLive,
95765067
C
119 mode,
120 p2pEnabled: options.common.p2pEnabled
ff563914 121 })
10475dea 122
3e254de8
C
123 player.on('p2pInfo', (_, data: PlayerNetworkInfo) => {
124 if (data.source !== 'p2p-media-loader' || isNaN(data.bandwidthEstimate)) return
125
126 saveAverageBandwidth(data.bandwidthEstimate)
127 })
128
2adfc7ea
C
129 return res(player)
130 })
131 })
132 }
133
c4207f97
C
134 private static async tryToRecoverHLSError (err: any, currentPlayer: videojs.Player, options: PeertubePlayerManagerOptions) {
135 if (err.code === 3) { // Decode error
6ec0b75b 136
c4207f97
C
137 // Display a notification to user
138 if (this.videojsDecodeErrors === 0) {
139 options.common.errorNotifier(currentPlayer.localize('The video failed to play, will try to fast forward.'))
2adfc7ea 140 }
a950e4c8 141
c4207f97
C
142 if (this.videojsDecodeErrors === 20) {
143 this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options)
144 return
2adfc7ea 145 }
39aad8cc 146
c4207f97 147 console.log('Fast forwarding HLS to recover from an error.')
dca0fe12 148
c4207f97 149 this.videojsDecodeErrors++
39aad8cc 150
c4207f97
C
151 options.common.startTime = currentPlayer.currentTime() + 2
152 options.common.autoplay = true
153 this.rebuildAndUpdateVideoElement(currentPlayer, options.common)
39aad8cc 154
c4207f97
C
155 const newPlayer = await this.buildPlayer('p2p-media-loader', options)
156 this.onPlayerChange(newPlayer)
157 } else {
158 this.maybeFallbackToWebTorrent('p2p-media-loader', currentPlayer, options)
2adfc7ea 159 }
39aad8cc
C
160 }
161
c4207f97
C
162 private static async maybeFallbackToWebTorrent (
163 currentMode: PlayerMode,
164 currentPlayer: videojs.Player,
165 options: PeertubePlayerManagerOptions
166 ) {
167 if (options.webtorrent.videoFiles.length === 0 || currentMode === 'webtorrent') {
168 currentPlayer.peertube().displayFatalError()
169 return
1dc240a9
RK
170 }
171
c4207f97 172 console.log('Fallback to webtorrent.')
2adfc7ea 173
c4207f97 174 this.rebuildAndUpdateVideoElement(currentPlayer, options.common)
2adfc7ea 175
c4207f97 176 await import('./webtorrent/webtorrent-plugin')
2adfc7ea 177
c4207f97
C
178 const newPlayer = await this.buildPlayer('webtorrent', options)
179 this.onPlayerChange(newPlayer)
2adfc7ea
C
180 }
181
c4207f97
C
182 private static rebuildAndUpdateVideoElement (player: videojs.Player, commonOptions: CommonOptions) {
183 const newVideoElement = document.createElement('video')
184 newVideoElement.className = this.playerElementClassName
9162fdd3 185
c4207f97
C
186 // VideoJS wraps our video element inside a div
187 let currentParentPlayerElement = commonOptions.playerElement.parentNode
188 // Fix on IOS, don't ask me why
189 if (!currentParentPlayerElement) currentParentPlayerElement = document.getElementById(commonOptions.playerElement.id).parentNode
a472cf03 190
c4207f97 191 currentParentPlayerElement.parentNode.insertBefore(newVideoElement, currentParentPlayerElement)
2adfc7ea 192
c4207f97
C
193 commonOptions.playerElement = newVideoElement
194 commonOptions.onPlayerElementChange(newVideoElement)
ff563914 195
c4207f97 196 player.dispose()
2adfc7ea 197
c4207f97 198 return newVideoElement
2adfc7ea
C
199 }
200
c4207f97
C
201 private static addContextMenu (optionsBuilder: PeertubePlayerOptionsBuilder, player: videojs.Player, commonOptions: CommonOptions) {
202 const options = optionsBuilder.getContextMenuOptions(player, commonOptions)
64228474 203
c4207f97 204 player.contextmenuUI(options)
64228474 205 }
2adfc7ea
C
206}
207
208// ############################################################################
209
210export {
211 videojs
212}