1 import { peertubeTranslate } from '../../../../../shared/core-utils/i18n'
10 VideoStreamingPlaylistType
11 } from '../../../../../shared/models'
12 import { P2PMediaLoaderOptions, PeertubePlayerManagerOptions, PlayerMode, VideoJSCaption } from '../../../assets/player'
21 } from '../../../root-helpers'
22 import { PeerTubePlugin } from './peertube-plugin'
23 import { PlayerHTML } from './player-html'
24 import { PlaylistTracker } from './playlist-tracker'
25 import { Translations } from './translations'
26 import { VideoFetcher } from './video-fetcher'
28 export class PlayerManagerOptions {
29 private autoplay: boolean
31 private controls: boolean
32 private controlBar: boolean
34 private muted: boolean
36 private subtitle: string
37 private enableApi = false
38 private startTime: number | string = 0
39 private stopTime: number | string
41 private title: boolean
42 private warningTitle: boolean
43 private peertubeLink: boolean
44 private p2pEnabled: boolean
45 private bigPlayBackgroundColor: string
46 private foregroundColor: string
48 private mode: PlayerMode
49 private scope = 'peertube'
52 private readonly playerHTML: PlayerHTML,
53 private readonly videoFetcher: VideoFetcher,
54 private readonly peertubePlugin: PeerTubePlugin
74 return this.warningTitle
78 return !!this.p2pEnabled
81 hasBigPlayBackgroundColor () {
82 return !!this.bigPlayBackgroundColor
85 getBigPlayBackgroundColor () {
86 return this.bigPlayBackgroundColor
89 hasForegroundColor () {
90 return !!this.foregroundColor
93 getForegroundColor () {
94 return this.foregroundColor
105 // ---------------------------------------------------------------------------
107 loadParams (config: HTMLServerConfig, video: VideoDetails) {
109 const params = new URL(window.location.toString()).searchParams
111 this.autoplay = getParamToggle(params, 'autoplay', false)
112 // Disable auto play on live videos that are not streamed
113 if (video.state.id === VideoState.LIVE_ENDED || video.state.id === VideoState.WAITING_FOR_LIVE) {
114 this.autoplay = false
117 this.controls = getParamToggle(params, 'controls', true)
118 this.controlBar = getParamToggle(params, 'controlBar', true)
120 this.muted = getParamToggle(params, 'muted', undefined)
121 this.loop = getParamToggle(params, 'loop', false)
122 this.title = getParamToggle(params, 'title', true)
123 this.enableApi = getParamToggle(params, 'api', this.enableApi)
124 this.warningTitle = getParamToggle(params, 'warningTitle', true)
125 this.peertubeLink = getParamToggle(params, 'peertubeLink', true)
126 this.p2pEnabled = getParamToggle(params, 'p2p', this.isP2PEnabled(config, video))
128 this.scope = getParamString(params, 'scope', this.scope)
129 this.subtitle = getParamString(params, 'subtitle')
130 this.startTime = getParamString(params, 'start')
131 this.stopTime = getParamString(params, 'stop')
133 this.bigPlayBackgroundColor = getParamString(params, 'bigPlayBackgroundColor')
134 this.foregroundColor = getParamString(params, 'foregroundColor')
136 const modeParam = getParamString(params, 'mode')
139 if (modeParam === 'p2p-media-loader') this.mode = 'p2p-media-loader'
140 else this.mode = 'webtorrent'
142 if (Array.isArray(video.streamingPlaylists) && video.streamingPlaylists.length !== 0) this.mode = 'p2p-media-loader'
143 else this.mode = 'webtorrent'
146 logger.error('Cannot get params from URL.', err)
150 // ---------------------------------------------------------------------------
152 async getPlayerOptions (options: {
154 captionsResponse: Response
157 serverConfig: HTMLServerConfig
159 alreadyHadPlayer: boolean
161 translations: Translations
163 playlistTracker?: PlaylistTracker
164 playNextPlaylistVideo?: () => any
165 playPreviousPlaylistVideo?: () => any
166 onVideoUpdate?: (uuid: string) => any
178 const videoCaptions = await this.buildCaptions(captionsResponse, translations)
180 const playerOptions: PeertubePlayerManagerOptions = {
182 // Autoplay in playlist mode
183 autoplay: alreadyHadPlayer ? true : this.autoplay,
185 controls: this.controls,
186 controlBar: this.controlBar,
191 p2pEnabled: this.p2pEnabled,
193 captions: videoCaptions.length !== 0,
194 subtitle: this.subtitle,
196 startTime: playlistTracker
197 ? playlistTracker.getCurrentElement().startTimestamp
199 stopTime: playlistTracker
200 ? playlistTracker.getCurrentElement().stopTimestamp
204 inactivityTimeout: 2500,
205 videoViewUrl: this.videoFetcher.getVideoViewsUrl(video.uuid),
207 videoShortUUID: video.shortUUID,
208 videoUUID: video.uuid,
210 playerElement: this.playerHTML.getPlayerElement(),
211 onPlayerElementChange: (element: HTMLVideoElement) => {
212 this.playerHTML.setPlayerElement(element)
215 videoDuration: video.duration,
218 peertubeLink: this.peertubeLink,
219 instanceName: serverConfig.instance.name,
221 poster: window.location.origin + video.previewPath,
222 theaterButton: false,
224 serverUrl: window.location.origin,
225 language: navigator.language,
226 embedUrl: window.location.origin + video.embedPath,
227 embedTitle: video.name,
229 errorNotifier: () => {
230 // Empty, we don't have a notifier in the embed
233 ...this.buildLiveOptions(video, live),
235 ...this.buildPlaylistOptions(options)
239 videoFiles: video.files
242 ...this.buildP2PMediaLoaderOptions(video),
244 pluginsManager: this.peertubePlugin.getPluginsManager()
250 private buildLiveOptions (video: VideoDetails, live: LiveVideo) {
251 if (!video.isLive) return { isLive: false }
256 latencyMode: live.latencyMode
261 private buildPlaylistOptions (options: {
262 playlistTracker?: PlaylistTracker
263 playNextPlaylistVideo?: () => any
264 playPreviousPlaylistVideo?: () => any
265 onVideoUpdate?: (uuid: string) => any
267 const { playlistTracker, playNextPlaylistVideo, playPreviousPlaylistVideo, onVideoUpdate } = options
269 if (!playlistTracker) return {}
273 elements: playlistTracker.getPlaylistElements(),
274 playlist: playlistTracker.getPlaylist(),
276 getCurrentPosition: () => playlistTracker.getCurrentPosition(),
278 onItemClicked: (videoPlaylistElement: VideoPlaylistElement) => {
279 playlistTracker.setCurrentElement(videoPlaylistElement)
281 onVideoUpdate(videoPlaylistElement.video.uuid)
285 nextVideo: () => playNextPlaylistVideo(),
286 hasNextVideo: () => playlistTracker.hasNextPlaylistElement(),
288 previousVideo: () => playPreviousPlaylistVideo(),
289 hasPreviousVideo: () => playlistTracker.hasPreviousPlaylistElement()
293 private buildP2PMediaLoaderOptions (video: VideoDetails) {
294 if (this.mode !== 'p2p-media-loader') return {}
296 const hlsPlaylist = video.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
300 playlistUrl: hlsPlaylist.playlistUrl,
301 segmentsSha256Url: hlsPlaylist.segmentsSha256Url,
302 redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl),
303 trackerAnnounce: video.trackerUrls,
304 videoFiles: hlsPlaylist.files
305 } as P2PMediaLoaderOptions
309 // ---------------------------------------------------------------------------
311 private async buildCaptions (captionsResponse: Response, translations: Translations): Promise<VideoJSCaption[]> {
312 if (captionsResponse.ok) {
313 const { data } = await captionsResponse.json()
315 return data.map((c: VideoCaption) => ({
316 label: peertubeTranslate(c.language.label, translations),
317 language: c.language.id,
318 src: window.location.origin + c.captionPath
325 // ---------------------------------------------------------------------------
327 private isP2PEnabled (config: HTMLServerConfig, video: Video) {
328 const userP2PEnabled = getBoolOrDefault(
329 peertubeLocalStorage.getItem(UserLocalStorageKeys.P2P_ENABLED),
330 config.defaults.p2p.embed.enabled
333 return isP2PEnabled(video, config, userP2PEnabled)