]>
Commit | Line | Data |
---|---|---|
2adfc7ea C |
1 | import { VideoFile } from '../../../../shared/models/videos' |
2 | // @ts-ignore | |
3 | import * as videojs from 'video.js' | |
4 | import 'videojs-hotkeys' | |
5 | import 'videojs-dock' | |
6 | import 'videojs-contextmenu-ui' | |
7 | import 'videojs-contrib-quality-levels' | |
3bcb4fd7 | 8 | import './upnext/upnext-plugin' |
62ab565d | 9 | import './bezels/bezels-plugin' |
2adfc7ea C |
10 | import './peertube-plugin' |
11 | import './videojs-components/peertube-link-button' | |
12 | import './videojs-components/resolution-menu-button' | |
13 | import './videojs-components/settings-menu-button' | |
14 | import './videojs-components/p2p-info-button' | |
15 | import './videojs-components/peertube-load-progress-bar' | |
16 | import './videojs-components/theater-button' | |
17 | import { P2PMediaLoaderPluginOptions, UserWatching, VideoJSCaption, VideoJSPluginOptions, videojsUntyped } from './peertube-videojs-typings' | |
09209296 | 18 | import { buildVideoEmbed, buildVideoLink, copyToClipboard, getRtcConfig } from './utils' |
3f9c4955 | 19 | import { isDefaultLocale } from '../../../../shared/models/i18n/i18n' |
09209296 C |
20 | import { segmentValidatorFactory } from './p2p-media-loader/segment-validator' |
21 | import { segmentUrlBuilderFactory } from './p2p-media-loader/segment-url-builder' | |
da332417 | 22 | import { RedundancyUrlManager } from './p2p-media-loader/redundancy-url-manager' |
43c66a91 | 23 | import { getStoredP2PEnabled } from './peertube-player-local-storage' |
3f9c4955 | 24 | import { TranslationsManager } from './translations-manager' |
2adfc7ea C |
25 | |
26 | // Change 'Playback Rate' to 'Speed' (smaller for our settings menu) | |
27 | videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed' | |
28 | // Change Captions to Subtitles/CC | |
29 | videojsUntyped.getComponent('CaptionsButton').prototype.controlText_ = 'Subtitles/CC' | |
30 | // We just want to display 'Off' instead of 'captions off', keep a space so the variable == true (hacky I know) | |
31 | videojsUntyped.getComponent('CaptionsButton').prototype.label_ = ' ' | |
32 | ||
3b6f205c | 33 | export type PlayerMode = 'webtorrent' | 'p2p-media-loader' |
2adfc7ea | 34 | |
3b6f205c | 35 | export type WebtorrentOptions = { |
2adfc7ea C |
36 | videoFiles: VideoFile[] |
37 | } | |
38 | ||
3b6f205c | 39 | export type P2PMediaLoaderOptions = { |
2adfc7ea | 40 | playlistUrl: string |
09209296 | 41 | segmentsSha256Url: string |
4348a27d | 42 | trackerAnnounce: string[] |
09209296 C |
43 | redundancyBaseUrls: string[] |
44 | videoFiles: VideoFile[] | |
2adfc7ea C |
45 | } |
46 | ||
5efab546 C |
47 | export interface CustomizationOptions { |
48 | startTime: number | string | |
49 | stopTime: number | string | |
50 | ||
51 | controls?: boolean | |
52 | muted?: boolean | |
53 | loop?: boolean | |
54 | subtitle?: string | |
96f6278f | 55 | resume?: string |
5efab546 C |
56 | |
57 | peertubeLink: boolean | |
58 | } | |
59 | ||
60 | export interface CommonOptions extends CustomizationOptions { | |
2adfc7ea | 61 | playerElement: HTMLVideoElement |
6ec0b75b | 62 | onPlayerElementChange: (element: HTMLVideoElement) => void |
2adfc7ea C |
63 | |
64 | autoplay: boolean | |
65 | videoDuration: number | |
66 | enableHotkeys: boolean | |
67 | inactivityTimeout: number | |
68 | poster: string | |
2adfc7ea | 69 | |
3d9a63d3 | 70 | theaterButton: boolean |
2adfc7ea | 71 | captions: boolean |
2adfc7ea C |
72 | |
73 | videoViewUrl: string | |
74 | embedUrl: string | |
75 | ||
76 | language?: string | |
2adfc7ea C |
77 | |
78 | videoCaptions: VideoJSCaption[] | |
79 | ||
80 | userWatching?: UserWatching | |
81 | ||
82 | serverUrl: string | |
83 | } | |
84 | ||
85 | export type PeertubePlayerManagerOptions = { | |
86 | common: CommonOptions, | |
6ec0b75b | 87 | webtorrent: WebtorrentOptions, |
2adfc7ea C |
88 | p2pMediaLoader?: P2PMediaLoaderOptions |
89 | } | |
90 | ||
91 | export class PeertubePlayerManager { | |
6ec0b75b | 92 | private static playerElementClassName: string |
bfbd9128 | 93 | private static onPlayerChange: (player: any) => void |
2adfc7ea | 94 | |
bfbd9128 | 95 | static async initialize (mode: PlayerMode, options: PeertubePlayerManagerOptions, onPlayerChange: (player: any) => void) { |
4348a27d C |
96 | let p2pMediaLoader: any |
97 | ||
bfbd9128 | 98 | this.onPlayerChange = onPlayerChange |
6ec0b75b C |
99 | this.playerElementClassName = options.common.playerElement.className |
100 | ||
09209296 | 101 | if (mode === 'webtorrent') await import('./webtorrent/webtorrent-plugin') |
4348a27d C |
102 | if (mode === 'p2p-media-loader') { |
103 | [ p2pMediaLoader ] = await Promise.all([ | |
104 | import('p2p-media-loader-hlsjs'), | |
09209296 | 105 | import('./p2p-media-loader/p2p-media-loader-plugin') |
4348a27d C |
106 | ]) |
107 | } | |
2adfc7ea | 108 | |
4348a27d | 109 | const videojsOptions = this.getVideojsOptions(mode, options, p2pMediaLoader) |
2adfc7ea | 110 | |
3f9c4955 | 111 | await TranslationsManager.loadLocaleInVideoJS(options.common.serverUrl, options.common.language, videojs) |
2adfc7ea C |
112 | |
113 | const self = this | |
114 | return new Promise(res => { | |
115 | videojs(options.common.playerElement, videojsOptions, function (this: any) { | |
116 | const player = this | |
117 | ||
536598cf C |
118 | let alreadyFallback = false |
119 | ||
120 | player.tech_.one('error', () => { | |
121 | if (!alreadyFallback) self.maybeFallbackToWebTorrent(mode, player, options) | |
122 | alreadyFallback = true | |
123 | }) | |
124 | ||
125 | player.one('error', () => { | |
126 | if (!alreadyFallback) self.maybeFallbackToWebTorrent(mode, player, options) | |
127 | alreadyFallback = true | |
128 | }) | |
6ec0b75b | 129 | |
2adfc7ea C |
130 | self.addContextMenu(mode, player, options.common.embedUrl) |
131 | ||
10475dea RK |
132 | player.bezels() |
133 | ||
2adfc7ea C |
134 | return res(player) |
135 | }) | |
136 | }) | |
137 | } | |
138 | ||
96cb4527 C |
139 | private static async maybeFallbackToWebTorrent (currentMode: PlayerMode, player: any, options: PeertubePlayerManagerOptions) { |
140 | if (currentMode === 'webtorrent') return | |
141 | ||
142 | console.log('Fallback to webtorrent.') | |
143 | ||
6ec0b75b C |
144 | const newVideoElement = document.createElement('video') |
145 | newVideoElement.className = this.playerElementClassName | |
146 | ||
147 | // VideoJS wraps our video element inside a div | |
96cb4527 C |
148 | let currentParentPlayerElement = options.common.playerElement.parentNode |
149 | // Fix on IOS, don't ask me why | |
150 | if (!currentParentPlayerElement) currentParentPlayerElement = document.getElementById(options.common.playerElement.id).parentNode | |
151 | ||
6ec0b75b C |
152 | currentParentPlayerElement.parentNode.insertBefore(newVideoElement, currentParentPlayerElement) |
153 | ||
154 | options.common.playerElement = newVideoElement | |
155 | options.common.onPlayerElementChange(newVideoElement) | |
156 | ||
157 | player.dispose() | |
158 | ||
159 | await import('./webtorrent/webtorrent-plugin') | |
160 | ||
161 | const mode = 'webtorrent' | |
162 | const videojsOptions = this.getVideojsOptions(mode, options) | |
163 | ||
164 | const self = this | |
165 | videojs(newVideoElement, videojsOptions, function (this: any) { | |
166 | const player = this | |
167 | ||
168 | self.addContextMenu(mode, player, options.common.embedUrl) | |
bfbd9128 C |
169 | |
170 | PeertubePlayerManager.onPlayerChange(player) | |
6ec0b75b C |
171 | }) |
172 | } | |
173 | ||
4348a27d | 174 | private static getVideojsOptions (mode: PlayerMode, options: PeertubePlayerManagerOptions, p2pMediaLoaderModule?: any) { |
2adfc7ea | 175 | const commonOptions = options.common |
09209296 | 176 | |
39aad8cc | 177 | let autoplay = commonOptions.autoplay |
3b6f205c | 178 | let html5 = {} |
2adfc7ea C |
179 | |
180 | const plugins: VideoJSPluginOptions = { | |
181 | peertube: { | |
09209296 C |
182 | mode, |
183 | autoplay, // Use peertube plugin autoplay because we get the file by webtorrent | |
2adfc7ea C |
184 | videoViewUrl: commonOptions.videoViewUrl, |
185 | videoDuration: commonOptions.videoDuration, | |
2adfc7ea C |
186 | userWatching: commonOptions.userWatching, |
187 | subtitle: commonOptions.subtitle, | |
f0a39880 C |
188 | videoCaptions: commonOptions.videoCaptions, |
189 | stopTime: commonOptions.stopTime | |
2adfc7ea C |
190 | } |
191 | } | |
192 | ||
39aad8cc C |
193 | if (commonOptions.enableHotkeys === true) { |
194 | PeertubePlayerManager.addHotkeysOptions(plugins) | |
195 | } | |
09209296 | 196 | |
39aad8cc C |
197 | if (mode === 'p2p-media-loader') { |
198 | const { streamrootHls } = PeertubePlayerManager.addP2PMediaLoaderOptions(plugins, options, p2pMediaLoaderModule) | |
2adfc7ea | 199 | |
3b6f205c | 200 | html5 = streamrootHls.html5 |
2adfc7ea C |
201 | } |
202 | ||
6ec0b75b | 203 | if (mode === 'webtorrent') { |
39aad8cc | 204 | PeertubePlayerManager.addWebTorrentOptions(plugins, options) |
09209296 C |
205 | |
206 | // WebTorrent plugin handles autoplay, because we do some hackish stuff in there | |
207 | autoplay = false | |
2adfc7ea C |
208 | } |
209 | ||
210 | const videojsOptions = { | |
3b6f205c C |
211 | html5, |
212 | ||
2adfc7ea C |
213 | // We don't use text track settings for now |
214 | textTrackSettings: false, | |
215 | controls: commonOptions.controls !== undefined ? commonOptions.controls : true, | |
216 | loop: commonOptions.loop !== undefined ? commonOptions.loop : false, | |
217 | ||
218 | muted: commonOptions.muted !== undefined | |
219 | ? commonOptions.muted | |
220 | : undefined, // Undefined so the player knows it has to check the local storage | |
221 | ||
39aad8cc C |
222 | autoplay: autoplay === true |
223 | ? 'any' // Use 'any' instead of true to get notifier by videojs if autoplay fails | |
224 | : autoplay, | |
225 | ||
2adfc7ea | 226 | poster: commonOptions.poster, |
2adfc7ea C |
227 | inactivityTimeout: commonOptions.inactivityTimeout, |
228 | playbackRates: [ 0.5, 0.75, 1, 1.25, 1.5, 2 ], | |
39aad8cc | 229 | |
2adfc7ea | 230 | plugins, |
39aad8cc | 231 | |
2adfc7ea C |
232 | controlBar: { |
233 | children: this.getControlBarChildren(mode, { | |
234 | captions: commonOptions.captions, | |
235 | peertubeLink: commonOptions.peertubeLink, | |
3d9a63d3 | 236 | theaterButton: commonOptions.theaterButton |
2adfc7ea C |
237 | }) |
238 | } | |
239 | } | |
240 | ||
39aad8cc C |
241 | if (commonOptions.language && !isDefaultLocale(commonOptions.language)) { |
242 | Object.assign(videojsOptions, { language: commonOptions.language }) | |
243 | } | |
2adfc7ea | 244 | |
39aad8cc C |
245 | return videojsOptions |
246 | } | |
2adfc7ea | 247 | |
39aad8cc C |
248 | private static addP2PMediaLoaderOptions ( |
249 | plugins: VideoJSPluginOptions, | |
250 | options: PeertubePlayerManagerOptions, | |
251 | p2pMediaLoaderModule: any | |
252 | ) { | |
253 | const p2pMediaLoaderOptions = options.p2pMediaLoader | |
254 | const commonOptions = options.common | |
255 | ||
256 | const trackerAnnounce = p2pMediaLoaderOptions.trackerAnnounce | |
257 | .filter(t => t.startsWith('ws')) | |
258 | ||
259 | const redundancyUrlManager = new RedundancyUrlManager(options.p2pMediaLoader.redundancyBaseUrls) | |
260 | ||
261 | const p2pMediaLoader: P2PMediaLoaderPluginOptions = { | |
262 | redundancyUrlManager, | |
263 | type: 'application/x-mpegURL', | |
264 | startTime: commonOptions.startTime, | |
265 | src: p2pMediaLoaderOptions.playlistUrl | |
266 | } | |
267 | ||
268 | let consumeOnly = false | |
269 | // FIXME: typings | |
fe9d0531 | 270 | if (navigator && (navigator as any).connection && (navigator as any).connection.type === 'cellular') { |
39aad8cc C |
271 | console.log('We are on a cellular connection: disabling seeding.') |
272 | consumeOnly = true | |
273 | } | |
274 | ||
275 | const p2pMediaLoaderConfig = { | |
276 | loader: { | |
277 | trackerAnnounce, | |
278 | segmentValidator: segmentValidatorFactory(options.p2pMediaLoader.segmentsSha256Url), | |
279 | rtcConfig: getRtcConfig(), | |
280 | requiredSegmentsPriority: 5, | |
281 | segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager), | |
282 | useP2P: getStoredP2PEnabled(), | |
283 | consumeOnly | |
284 | }, | |
285 | segments: { | |
286 | swarmId: p2pMediaLoaderOptions.playlistUrl | |
287 | } | |
288 | } | |
289 | const streamrootHls = { | |
290 | levelLabelHandler: (level: { height: number, width: number }) => { | |
291 | const file = p2pMediaLoaderOptions.videoFiles.find(f => f.resolution.id === level.height) | |
292 | ||
293 | let label = file.resolution.label | |
294 | if (file.fps >= 50) label += file.fps | |
295 | ||
296 | return label | |
297 | }, | |
298 | html5: { | |
299 | hlsjsConfig: { | |
300 | capLevelToPlayerSize: true, | |
301 | autoStartLoad: false, | |
302 | liveSyncDurationCount: 7, | |
303 | loader: new p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass() | |
2adfc7ea | 304 | } |
39aad8cc | 305 | } |
2adfc7ea C |
306 | } |
307 | ||
39aad8cc C |
308 | const toAssign = { p2pMediaLoader, streamrootHls } |
309 | Object.assign(plugins, toAssign) | |
310 | ||
311 | return toAssign | |
312 | } | |
313 | ||
314 | private static addWebTorrentOptions (plugins: VideoJSPluginOptions, options: PeertubePlayerManagerOptions) { | |
315 | const commonOptions = options.common | |
316 | const webtorrentOptions = options.webtorrent | |
317 | ||
318 | const webtorrent = { | |
319 | autoplay: commonOptions.autoplay, | |
320 | videoDuration: commonOptions.videoDuration, | |
321 | playerElement: commonOptions.playerElement, | |
322 | videoFiles: webtorrentOptions.videoFiles, | |
323 | startTime: commonOptions.startTime | |
2adfc7ea C |
324 | } |
325 | ||
39aad8cc | 326 | Object.assign(plugins, { webtorrent }) |
2adfc7ea C |
327 | } |
328 | ||
329 | private static getControlBarChildren (mode: PlayerMode, options: { | |
330 | peertubeLink: boolean | |
3d9a63d3 | 331 | theaterButton: boolean, |
2adfc7ea C |
332 | captions: boolean |
333 | }) { | |
334 | const settingEntries = [] | |
335 | const loadProgressBar = mode === 'webtorrent' ? 'peerTubeLoadProgressBar' : 'loadProgressBar' | |
336 | ||
337 | // Keep an order | |
338 | settingEntries.push('playbackRateMenuButton') | |
339 | if (options.captions === true) settingEntries.push('captionsButton') | |
340 | settingEntries.push('resolutionMenuButton') | |
341 | ||
342 | const children = { | |
343 | 'playToggle': {}, | |
344 | 'currentTimeDisplay': {}, | |
345 | 'timeDivider': {}, | |
346 | 'durationDisplay': {}, | |
347 | 'liveDisplay': {}, | |
348 | ||
349 | 'flexibleWidthSpacer': {}, | |
350 | 'progressControl': { | |
351 | children: { | |
352 | 'seekBar': { | |
353 | children: { | |
354 | [loadProgressBar]: {}, | |
355 | 'mouseTimeDisplay': {}, | |
356 | 'playProgressBar': {} | |
357 | } | |
358 | } | |
359 | } | |
360 | }, | |
361 | ||
362 | 'p2PInfoButton': {}, | |
363 | ||
364 | 'muteToggle': {}, | |
365 | 'volumeControl': {}, | |
366 | ||
367 | 'settingsButton': { | |
368 | setup: { | |
369 | maxHeightOffset: 40 | |
370 | }, | |
371 | entries: settingEntries | |
372 | } | |
373 | } | |
374 | ||
375 | if (options.peertubeLink === true) { | |
376 | Object.assign(children, { | |
377 | 'peerTubeLinkButton': {} | |
378 | }) | |
379 | } | |
380 | ||
3d9a63d3 | 381 | if (options.theaterButton === true) { |
2adfc7ea C |
382 | Object.assign(children, { |
383 | 'theaterButton': {} | |
384 | }) | |
385 | } | |
386 | ||
387 | Object.assign(children, { | |
388 | 'fullscreenToggle': {} | |
389 | }) | |
390 | ||
391 | return children | |
392 | } | |
393 | ||
394 | private static addContextMenu (mode: PlayerMode, player: any, videoEmbedUrl: string) { | |
395 | const content = [ | |
396 | { | |
397 | label: player.localize('Copy the video URL'), | |
398 | listener: function () { | |
399 | copyToClipboard(buildVideoLink()) | |
400 | } | |
401 | }, | |
402 | { | |
403 | label: player.localize('Copy the video URL at the current time'), | |
404 | listener: function () { | |
405 | const player = this as videojs.Player | |
2f4c784a | 406 | copyToClipboard(buildVideoLink({ startTime: player.currentTime() })) |
2adfc7ea C |
407 | } |
408 | }, | |
409 | { | |
410 | label: player.localize('Copy embed code'), | |
411 | listener: () => { | |
412 | copyToClipboard(buildVideoEmbed(videoEmbedUrl)) | |
413 | } | |
414 | } | |
415 | ] | |
416 | ||
417 | if (mode === 'webtorrent') { | |
418 | content.push({ | |
419 | label: player.localize('Copy magnet URI'), | |
420 | listener: function () { | |
421 | const player = this as videojs.Player | |
422 | copyToClipboard(player.webtorrent().getCurrentVideoFile().magnetUri) | |
423 | } | |
424 | }) | |
425 | } | |
426 | ||
427 | player.contextmenuUI({ content }) | |
428 | } | |
429 | ||
39aad8cc C |
430 | private static addHotkeysOptions (plugins: VideoJSPluginOptions) { |
431 | Object.assign(plugins, { | |
432 | hotkeys: { | |
433 | enableVolumeScroll: false, | |
434 | enableModifiersForNumbers: false, | |
435 | ||
436 | fullscreenKey: function (event: KeyboardEvent) { | |
437 | // fullscreen with the f key or Ctrl+Enter | |
438 | return event.key === 'f' || (event.ctrlKey && event.key === 'Enter') | |
439 | }, | |
440 | ||
441 | seekStep: function (event: KeyboardEvent) { | |
442 | // mimic VLC seek behavior, and default to 5 (original value is 5). | |
443 | if (event.ctrlKey && event.altKey) { | |
444 | return 5 * 60 | |
445 | } else if (event.ctrlKey) { | |
446 | return 60 | |
447 | } else if (event.altKey) { | |
448 | return 10 | |
449 | } else { | |
450 | return 5 | |
451 | } | |
452 | }, | |
453 | ||
454 | customKeys: { | |
455 | increasePlaybackRateKey: { | |
456 | key: function (event: KeyboardEvent) { | |
457 | return event.key === '>' | |
458 | }, | |
459 | handler: function (player: videojs.Player) { | |
460 | player.playbackRate((player.playbackRate() + 0.1).toFixed(2)) | |
461 | } | |
462 | }, | |
463 | decreasePlaybackRateKey: { | |
464 | key: function (event: KeyboardEvent) { | |
465 | return event.key === '<' | |
466 | }, | |
467 | handler: function (player: videojs.Player) { | |
468 | player.playbackRate((player.playbackRate() - 0.1).toFixed(2)) | |
469 | } | |
470 | }, | |
471 | frameByFrame: { | |
472 | key: function (event: KeyboardEvent) { | |
473 | return event.key === '.' | |
474 | }, | |
475 | handler: function (player: videojs.Player) { | |
476 | player.pause() | |
477 | // Calculate movement distance (assuming 30 fps) | |
478 | const dist = 1 / 30 | |
479 | player.currentTime(player.currentTime() + dist) | |
480 | } | |
481 | } | |
482 | } | |
483 | } | |
484 | }) | |
485 | } | |
2adfc7ea C |
486 | } |
487 | ||
488 | // ############################################################################ | |
489 | ||
490 | export { | |
491 | videojs | |
492 | } |