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
41 private playbackRate: number | string
43 private title: boolean
44 private warningTitle: boolean
45 private peertubeLink: boolean
46 private p2pEnabled: boolean
47 private bigPlayBackgroundColor: string
48 private foregroundColor: string
50 private mode: PlayerMode
51 private scope = 'peertube'
54 private readonly playerHTML: PlayerHTML,
55 private readonly videoFetcher: VideoFetcher,
56 private readonly peertubePlugin: PeerTubePlugin
76 return this.warningTitle
80 return !!this.p2pEnabled
83 hasBigPlayBackgroundColor () {
84 return !!this.bigPlayBackgroundColor
87 getBigPlayBackgroundColor () {
88 return this.bigPlayBackgroundColor
91 hasForegroundColor () {
92 return !!this.foregroundColor
95 getForegroundColor () {
96 return this.foregroundColor
107 // ---------------------------------------------------------------------------
109 loadParams (config: HTMLServerConfig, video: VideoDetails) {
111 const params = new URL(window.location.toString()).searchParams
113 this.autoplay = getParamToggle(params, 'autoplay', false)
114 // Disable auto play on live videos that are not streamed
115 if (video.state.id === VideoState.LIVE_ENDED || video.state.id === VideoState.WAITING_FOR_LIVE) {
116 this.autoplay = false
119 this.controls = getParamToggle(params, 'controls', true)
120 this.controlBar = getParamToggle(params, 'controlBar', true)
122 this.muted = getParamToggle(params, 'muted', undefined)
123 this.loop = getParamToggle(params, 'loop', false)
124 this.title = getParamToggle(params, 'title', true)
125 this.enableApi = getParamToggle(params, 'api', this.enableApi)
126 this.warningTitle = getParamToggle(params, 'warningTitle', true)
127 this.peertubeLink = getParamToggle(params, 'peertubeLink', true)
128 this.p2pEnabled = getParamToggle(params, 'p2p', this.isP2PEnabled(config, video))
130 this.scope = getParamString(params, 'scope', this.scope)
131 this.subtitle = getParamString(params, 'subtitle')
132 this.startTime = getParamString(params, 'start')
133 this.stopTime = getParamString(params, 'stop')
134 this.playbackRate = getParamString(params, 'playbackRate')
136 this.bigPlayBackgroundColor = getParamString(params, 'bigPlayBackgroundColor')
137 this.foregroundColor = getParamString(params, 'foregroundColor')
139 const modeParam = getParamString(params, 'mode')
142 if (modeParam === 'p2p-media-loader') this.mode = 'p2p-media-loader'
143 else this.mode = 'webtorrent'
145 if (Array.isArray(video.streamingPlaylists) && video.streamingPlaylists.length !== 0) this.mode = 'p2p-media-loader'
146 else this.mode = 'webtorrent'
149 logger.error('Cannot get params from URL.', err)
153 // ---------------------------------------------------------------------------
155 async getPlayerOptions (options: {
157 captionsResponse: Response
160 forceAutoplay: boolean
162 authorizationHeader: () => string
163 videoFileToken: () => string
165 serverConfig: HTMLServerConfig
167 autoplayFromPreviousVideo: boolean
169 translations: Translations
171 playlistTracker?: PlaylistTracker
172 playNextPlaylistVideo?: () => any
173 playPreviousPlaylistVideo?: () => any
174 onVideoUpdate?: (uuid: string) => any
179 autoplayFromPreviousVideo,
189 const videoCaptions = await this.buildCaptions(captionsResponse, translations)
191 const playerOptions: PeertubePlayerManagerOptions = {
193 // Autoplay in playlist mode
194 autoplay: autoplayFromPreviousVideo ? true : this.autoplay,
197 controls: this.controls,
198 controlBar: this.controlBar,
203 p2pEnabled: this.p2pEnabled,
205 captions: videoCaptions.length !== 0,
206 subtitle: this.subtitle,
208 startTime: playlistTracker
209 ? playlistTracker.getCurrentElement().startTimestamp
211 stopTime: playlistTracker
212 ? playlistTracker.getCurrentElement().stopTimestamp
215 playbackRate: this.playbackRate,
218 inactivityTimeout: 2500,
219 videoViewUrl: this.videoFetcher.getVideoViewsUrl(video.uuid),
220 videoViewIntervalMs: 5000,
221 metricsUrl: window.location.origin + '/api/v1/metrics/playback',
223 videoShortUUID: video.shortUUID,
224 videoUUID: video.uuid,
226 playerElement: this.playerHTML.getPlayerElement(),
227 onPlayerElementChange: (element: HTMLVideoElement) => {
228 this.playerHTML.setPlayerElement(element)
231 videoDuration: video.duration,
234 peertubeLink: this.peertubeLink,
235 instanceName: serverConfig.instance.name,
237 poster: window.location.origin + video.previewPath,
238 theaterButton: false,
240 serverUrl: window.location.origin,
241 language: navigator.language,
242 embedUrl: window.location.origin + video.embedPath,
243 embedTitle: video.name,
245 requiresAuth: videoRequiresAuth(video),
249 errorNotifier: () => {
250 // Empty, we don't have a notifier in the embed
253 ...this.buildLiveOptions(video, live),
255 ...this.buildPlaylistOptions(options)
259 videoFiles: video.files
262 ...this.buildP2PMediaLoaderOptions(video),
264 pluginsManager: this.peertubePlugin.getPluginsManager()
270 private buildLiveOptions (video: VideoDetails, live: LiveVideo) {
271 if (!video.isLive) return { isLive: false }
276 latencyMode: live.latencyMode
281 private buildPlaylistOptions (options: {
282 playlistTracker?: PlaylistTracker
283 playNextPlaylistVideo?: () => any
284 playPreviousPlaylistVideo?: () => any
285 onVideoUpdate?: (uuid: string) => any
287 const { playlistTracker, playNextPlaylistVideo, playPreviousPlaylistVideo, onVideoUpdate } = options
289 if (!playlistTracker) return {}
293 elements: playlistTracker.getPlaylistElements(),
294 playlist: playlistTracker.getPlaylist(),
296 getCurrentPosition: () => playlistTracker.getCurrentPosition(),
298 onItemClicked: (videoPlaylistElement: VideoPlaylistElement) => {
299 playlistTracker.setCurrentElement(videoPlaylistElement)
301 onVideoUpdate(videoPlaylistElement.video.uuid)
305 nextVideo: () => playNextPlaylistVideo(),
306 hasNextVideo: () => playlistTracker.hasNextPlaylistElement(),
308 previousVideo: () => playPreviousPlaylistVideo(),
309 hasPreviousVideo: () => playlistTracker.hasPreviousPlaylistElement()
313 private buildP2PMediaLoaderOptions (video: VideoDetails) {
314 if (this.mode !== 'p2p-media-loader') return {}
316 const hlsPlaylist = video.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
320 playlistUrl: hlsPlaylist.playlistUrl,
321 segmentsSha256Url: hlsPlaylist.segmentsSha256Url,
322 redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl),
323 trackerAnnounce: video.trackerUrls,
324 videoFiles: hlsPlaylist.files
325 } as P2PMediaLoaderOptions
329 // ---------------------------------------------------------------------------
331 private async buildCaptions (captionsResponse: Response, translations: Translations): Promise<VideoJSCaption[]> {
332 if (captionsResponse.ok) {
333 const { data } = await captionsResponse.json()
335 return data.map((c: VideoCaption) => ({
336 label: peertubeTranslate(c.language.label, translations),
337 language: c.language.id,
338 src: window.location.origin + c.captionPath
345 // ---------------------------------------------------------------------------
347 private isP2PEnabled (config: HTMLServerConfig, video: Video) {
348 const userP2PEnabled = getBoolOrDefault(
349 peertubeLocalStorage.getItem(UserLocalStorageKeys.P2P_ENABLED),
350 config.defaults.p2p.embed.enabled
353 return isP2PEnabled(video, config, userP2PEnabled)