+ try {
+ this.player.dispose()
+ this.player = undefined
+ } catch (err) {
+ console.error('Cannot dispose player.', err)
+ }
+ }
+ }
+
+ private buildPlayerManagerOptions (params: {
+ video: VideoDetails,
+ videoCaptions: VideoCaption[],
+ urlOptions: CustomizationOptions & { playerMode: PlayerMode },
+ user?: AuthUser
+ }) {
+ const { video, videoCaptions, urlOptions, user } = params
+
+ let startTime = timeToInt(urlOptions.startTime) || (video.userHistory ? video.userHistory.currentTime : 0)
+ // If we are at the end of the video, reset the timer
+ if (video.duration - startTime <= 1) startTime = 0
+
+ const playerCaptions = videoCaptions.map(c => ({
+ label: c.language.label,
+ language: c.language.id,
+ src: environment.apiUrl + c.captionPath
+ }))
+
+ const options: PeertubePlayerManagerOptions = {
+ common: {
+ autoplay: this.isAutoplay(),
+
+ playerElement: this.playerElement,
+ onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element,
+
+ videoDuration: video.duration,
+ enableHotkeys: true,
+ inactivityTimeout: 2500,
+ poster: video.previewUrl,
+
+ startTime,
+ stopTime: urlOptions.stopTime,
+ controls: urlOptions.controls,
+ muted: urlOptions.muted,
+ loop: urlOptions.loop,
+ subtitle: urlOptions.subtitle,
+
+ peertubeLink: urlOptions.peertubeLink,
+
+ theaterButton: true,
+ captions: videoCaptions.length !== 0,
+
+ videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE
+ ? this.videoService.getVideoViewUrl(video.uuid)
+ : null,
+ embedUrl: video.embedUrl,
+
+ language: this.localeId,
+
+ userWatching: user && user.videosHistoryEnabled === true ? {
+ url: this.videoService.getUserWatchingVideoUrl(video.uuid),
+ authorizationHeader: this.authService.getRequestHeaderValue()
+ } : undefined,
+
+ serverUrl: environment.apiUrl,
+
+ videoCaptions: playerCaptions
+ },
+
+ webtorrent: {
+ videoFiles: video.files
+ }
+ }
+
+ let mode: PlayerMode
+
+ if (urlOptions.playerMode) {
+ if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader'
+ else mode = 'webtorrent'
+ } else {
+ if (video.hasHlsPlaylist()) mode = 'p2p-media-loader'
+ else mode = 'webtorrent'
+ }
+
+ if (mode === 'p2p-media-loader') {
+ const hlsPlaylist = video.getHlsPlaylist()
+
+ const p2pMediaLoader = {
+ playlistUrl: hlsPlaylist.playlistUrl,
+ segmentsSha256Url: hlsPlaylist.segmentsSha256Url,
+ redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl),
+ trackerAnnounce: video.trackerUrls,
+ videoFiles: hlsPlaylist.files
+ } as P2PMediaLoaderOptions
+
+ Object.assign(options, { p2pMediaLoader })