1 import { peertubeTranslate } from '../../../../../shared/core-utils/i18n'
10 VideoStreamingPlaylistType
11 } from '../../../../../shared/models'
12 import { P2PMediaLoaderOptions, PeertubePlayerManagerOptions, PlayerMode, VideoJSCaption } from '../../../assets/player'
22 } from '../../../root-helpers'
23 import { PeerTubePlugin } from './peertube-plugin'
24 import { PlayerHTML } from './player-html'
25 import { PlaylistTracker } from './playlist-tracker'
26 import { Translations } from './translations'
27 import { VideoFetcher } from './video-fetcher'
29 export class PlayerManagerOptions {
30 private autoplay: boolean
32 private controls: boolean
33 private controlBar: boolean
35 private muted: boolean
37 private subtitle: string
38 private enableApi = false
39 private startTime: number | string = 0
40 private stopTime: number | string
42 private title: boolean
43 private warningTitle: boolean
44 private peertubeLink: boolean
45 private p2pEnabled: boolean
46 private bigPlayBackgroundColor: string
47 private foregroundColor: string
49 private mode: PlayerMode
50 private scope = 'peertube'
53 private readonly playerHTML: PlayerHTML,
54 private readonly videoFetcher: VideoFetcher,
55 private readonly peertubePlugin: PeerTubePlugin
75 return this.warningTitle
79 return !!this.p2pEnabled
82 hasBigPlayBackgroundColor () {
83 return !!this.bigPlayBackgroundColor
86 getBigPlayBackgroundColor () {
87 return this.bigPlayBackgroundColor
90 hasForegroundColor () {
91 return !!this.foregroundColor
94 getForegroundColor () {
95 return this.foregroundColor
106 // ---------------------------------------------------------------------------
108 loadParams (config: HTMLServerConfig, video: VideoDetails) {
110 const params = new URL(window.location.toString()).searchParams
112 this.autoplay = getParamToggle(params, 'autoplay', false)
113 // Disable auto play on live videos that are not streamed
114 if (video.state.id === VideoState.LIVE_ENDED || video.state.id === VideoState.WAITING_FOR_LIVE) {
115 this.autoplay = false
118 this.controls = getParamToggle(params, 'controls', true)
119 this.controlBar = getParamToggle(params, 'controlBar', true)
121 this.muted = getParamToggle(params, 'muted', undefined)
122 this.loop = getParamToggle(params, 'loop', false)
123 this.title = getParamToggle(params, 'title', true)
124 this.enableApi = getParamToggle(params, 'api', this.enableApi)
125 this.warningTitle = getParamToggle(params, 'warningTitle', true)
126 this.peertubeLink = getParamToggle(params, 'peertubeLink', true)
127 this.p2pEnabled = getParamToggle(params, 'p2p', this.isP2PEnabled(config, video))
129 this.scope = getParamString(params, 'scope', this.scope)
130 this.subtitle = getParamString(params, 'subtitle')
131 this.startTime = getParamString(params, 'start')
132 this.stopTime = getParamString(params, 'stop')
134 this.bigPlayBackgroundColor = getParamString(params, 'bigPlayBackgroundColor')
135 this.foregroundColor = getParamString(params, 'foregroundColor')
137 const modeParam = getParamString(params, 'mode')
140 if (modeParam === 'p2p-media-loader') this.mode = 'p2p-media-loader'
141 else this.mode = 'webtorrent'
143 if (Array.isArray(video.streamingPlaylists) && video.streamingPlaylists.length !== 0) this.mode = 'p2p-media-loader'
144 else this.mode = 'webtorrent'
147 logger.error('Cannot get params from URL.', err)
151 // ---------------------------------------------------------------------------
153 async getPlayerOptions (options: {
155 captionsResponse: Response
158 forceAutoplay: boolean
160 authorizationHeader: () => string
161 videoFileToken: () => string
163 serverConfig: HTMLServerConfig
165 alreadyHadPlayer: boolean
167 translations: Translations
169 playlistTracker?: PlaylistTracker
170 playNextPlaylistVideo?: () => any
171 playPreviousPlaylistVideo?: () => any
172 onVideoUpdate?: (uuid: string) => any
187 const videoCaptions = await this.buildCaptions(captionsResponse, translations)
189 const playerOptions: PeertubePlayerManagerOptions = {
191 // Autoplay in playlist mode
192 autoplay: alreadyHadPlayer ? true : this.autoplay,
195 controls: this.controls,
196 controlBar: this.controlBar,
201 p2pEnabled: this.p2pEnabled,
203 captions: videoCaptions.length !== 0,
204 subtitle: this.subtitle,
206 startTime: playlistTracker
207 ? playlistTracker.getCurrentElement().startTimestamp
209 stopTime: playlistTracker
210 ? playlistTracker.getCurrentElement().stopTimestamp
214 inactivityTimeout: 2500,
215 videoViewUrl: this.videoFetcher.getVideoViewsUrl(video.uuid),
216 metricsUrl: window.location.origin + '/api/v1/metrics/playback',
218 videoShortUUID: video.shortUUID,
219 videoUUID: video.uuid,
221 playerElement: this.playerHTML.getPlayerElement(),
222 onPlayerElementChange: (element: HTMLVideoElement) => {
223 this.playerHTML.setPlayerElement(element)
226 videoDuration: video.duration,
229 peertubeLink: this.peertubeLink,
230 instanceName: serverConfig.instance.name,
232 poster: window.location.origin + video.previewPath,
233 theaterButton: false,
235 serverUrl: window.location.origin,
236 language: navigator.language,
237 embedUrl: window.location.origin + video.embedPath,
238 embedTitle: video.name,
240 requiresAuth: videoRequiresAuth(video),
244 errorNotifier: () => {
245 // Empty, we don't have a notifier in the embed
248 ...this.buildLiveOptions(video, live),
250 ...this.buildPlaylistOptions(options)
254 videoFiles: video.files
257 ...this.buildP2PMediaLoaderOptions(video),
259 pluginsManager: this.peertubePlugin.getPluginsManager()
265 private buildLiveOptions (video: VideoDetails, live: LiveVideo) {
266 if (!video.isLive) return { isLive: false }
271 latencyMode: live.latencyMode
276 private buildPlaylistOptions (options: {
277 playlistTracker?: PlaylistTracker
278 playNextPlaylistVideo?: () => any
279 playPreviousPlaylistVideo?: () => any
280 onVideoUpdate?: (uuid: string) => any
282 const { playlistTracker, playNextPlaylistVideo, playPreviousPlaylistVideo, onVideoUpdate } = options
284 if (!playlistTracker) return {}
288 elements: playlistTracker.getPlaylistElements(),
289 playlist: playlistTracker.getPlaylist(),
291 getCurrentPosition: () => playlistTracker.getCurrentPosition(),
293 onItemClicked: (videoPlaylistElement: VideoPlaylistElement) => {
294 playlistTracker.setCurrentElement(videoPlaylistElement)
296 onVideoUpdate(videoPlaylistElement.video.uuid)
300 nextVideo: () => playNextPlaylistVideo(),
301 hasNextVideo: () => playlistTracker.hasNextPlaylistElement(),
303 previousVideo: () => playPreviousPlaylistVideo(),
304 hasPreviousVideo: () => playlistTracker.hasPreviousPlaylistElement()
308 private buildP2PMediaLoaderOptions (video: VideoDetails) {
309 if (this.mode !== 'p2p-media-loader') return {}
311 const hlsPlaylist = video.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
315 playlistUrl: hlsPlaylist.playlistUrl,
316 segmentsSha256Url: hlsPlaylist.segmentsSha256Url,
317 redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl),
318 trackerAnnounce: video.trackerUrls,
319 videoFiles: hlsPlaylist.files
320 } as P2PMediaLoaderOptions
324 // ---------------------------------------------------------------------------
326 private async buildCaptions (captionsResponse: Response, translations: Translations): Promise<VideoJSCaption[]> {
327 if (captionsResponse.ok) {
328 const { data } = await captionsResponse.json()
330 return data.map((c: VideoCaption) => ({
331 label: peertubeTranslate(c.language.label, translations),
332 language: c.language.id,
333 src: window.location.origin + c.captionPath
340 // ---------------------------------------------------------------------------
342 private isP2PEnabled (config: HTMLServerConfig, video: Video) {
343 const userP2PEnabled = getBoolOrDefault(
344 peertubeLocalStorage.getItem(UserLocalStorageKeys.P2P_ENABLED),
345 config.defaults.p2p.embed.enabled
348 return isP2PEnabled(video, config, userP2PEnabled)