]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
f443a746 | 2 | import { forkJoin, map, Observable, of, Subscription, switchMap } from 'rxjs' |
57d65032 | 3 | import { VideoJsPlayer } from 'video.js' |
67ed6552 | 4 | import { PlatformLocation } from '@angular/common' |
c894a1ea | 5 | import { Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' |
df98563e | 6 | import { ActivatedRoute, Router } from '@angular/router' |
a5cf76af C |
7 | import { |
8 | AuthService, | |
9 | AuthUser, | |
10 | ConfirmService, | |
0f01a8ba | 11 | MetaService, |
a5cf76af C |
12 | Notifier, |
13 | PeerTubeSocket, | |
72f611ca | 14 | PluginService, |
a5cf76af | 15 | RestExtractor, |
2666fd7c | 16 | ScreenService, |
a5cf76af | 17 | ServerService, |
a9bfa85d | 18 | User, |
a5cf76af C |
19 | UserService |
20 | } from '@app/core' | |
67ed6552 | 21 | import { HooksService } from '@app/core/plugins/hooks.service' |
4504f09f | 22 | import { isXPercentInViewport, scrollToTop } from '@app/helpers' |
67ed6552 C |
23 | import { Video, VideoCaptionService, VideoDetails, VideoService } from '@app/shared/shared-main' |
24 | import { SubscribeButtonComponent } from '@app/shared/shared-user-subscription' | |
f443a746 | 25 | import { LiveVideoService } from '@app/shared/shared-video-live' |
67ed6552 | 26 | import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist' |
42b40636 | 27 | import { logger } from '@root-helpers/logger' |
57d65032 | 28 | import { isP2PEnabled } from '@root-helpers/video' |
15a7eafb | 29 | import { timeToInt } from '@shared/core-utils' |
c0e8b12e C |
30 | import { |
31 | HTMLServerConfig, | |
32 | HttpStatusCode, | |
f443a746 | 33 | LiveVideo, |
c0e8b12e C |
34 | PeerTubeProblemDocument, |
35 | ServerErrorCode, | |
36 | VideoCaption, | |
37 | VideoPrivacy, | |
38 | VideoState | |
39 | } from '@shared/models' | |
6ec0b75b | 40 | import { |
5efab546 | 41 | CustomizationOptions, |
6ec0b75b C |
42 | P2PMediaLoaderOptions, |
43 | PeertubePlayerManager, | |
44 | PeertubePlayerManagerOptions, | |
67ed6552 C |
45 | PlayerMode, |
46 | videojs | |
c4207f97 C |
47 | } from '../../../assets/player' |
48 | import { cleanupVideoWatch, getStoredTheater, getStoredVideoWatchHistory } from '../../../assets/player/peertube-player-local-storage' | |
67ed6552 | 49 | import { environment } from '../../../environments/environment' |
911186da | 50 | import { VideoWatchPlaylistComponent } from './shared' |
dc8bc31b | 51 | |
a5cf76af C |
52 | type URLOptions = CustomizationOptions & { playerMode: PlayerMode } |
53 | ||
dc8bc31b C |
54 | @Component({ |
55 | selector: 'my-video-watch', | |
ec8d8440 C |
56 | templateUrl: './video-watch.component.html', |
57 | styleUrls: [ './video-watch.component.scss' ] | |
dc8bc31b | 58 | }) |
0629423c | 59 | export class VideoWatchComponent implements OnInit, OnDestroy { |
f36da21e | 60 | @ViewChild('videoWatchPlaylist', { static: true }) videoWatchPlaylist: VideoWatchPlaylistComponent |
2f5d2ec5 | 61 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent |
df98563e | 62 | |
57d65032 | 63 | player: VideoJsPlayer |
0826c92d | 64 | playerElement: HTMLVideoElement |
c15d61f5 | 65 | playerPlaceholderImgSrc: string |
c894a1ea | 66 | theaterEnabled = false |
2de96f4d | 67 | |
2f4c784a C |
68 | video: VideoDetails = null |
69 | videoCaptions: VideoCaption[] = [] | |
f443a746 | 70 | liveVideo: LiveVideo |
2f4c784a | 71 | |
d142c7b9 | 72 | playlistPosition: number |
e2f01c47 | 73 | playlist: VideoPlaylist = null |
e2f01c47 | 74 | |
6d88de72 | 75 | remoteServerDown = false |
5a2f775a | 76 | noPlaylistVideoFound = false |
c15d61f5 | 77 | |
c894a1ea | 78 | private nextVideoUUID = '' |
3bcb4fd7 | 79 | private nextVideoTitle = '' |
c894a1ea | 80 | |
f0a39880 | 81 | private currentTime: number |
c894a1ea | 82 | |
df98563e | 83 | private paramsSub: Subscription |
e2f01c47 | 84 | private queryParamsSub: Subscription |
31b6ddf8 | 85 | private configSub: Subscription |
a5cf76af | 86 | private liveVideosSub: Subscription |
df98563e | 87 | |
2989628b | 88 | private serverConfig: HTMLServerConfig |
ba430d75 | 89 | |
6ea59f41 C |
90 | private hotkeys: Hotkey[] = [] |
91 | ||
df98563e | 92 | constructor ( |
4fd8aa32 | 93 | private elementRef: ElementRef, |
0629423c | 94 | private route: ActivatedRoute, |
92fb909c | 95 | private router: Router, |
d3ef341a | 96 | private videoService: VideoService, |
e2f01c47 | 97 | private playlistService: VideoPlaylistService, |
f443a746 | 98 | private liveVideoService: LiveVideoService, |
92fb909c | 99 | private confirmService: ConfirmService, |
3ec343a4 | 100 | private metaService: MetaService, |
7ddd02c9 | 101 | private authService: AuthService, |
d3217560 | 102 | private userService: UserService, |
0883b324 | 103 | private serverService: ServerService, |
a51bad1a | 104 | private restExtractor: RestExtractor, |
f8b2c1b4 | 105 | private notifier: Notifier, |
901637bb | 106 | private zone: NgZone, |
16f7022b | 107 | private videoCaptionService: VideoCaptionService, |
20d21199 | 108 | private hotkeysService: HotkeysService, |
93cae479 | 109 | private hooks: HooksService, |
72f611ca | 110 | private pluginService: PluginService, |
a5cf76af | 111 | private peertubeSocket: PeerTubeSocket, |
2666fd7c | 112 | private screenService: ScreenService, |
60c2bc80 | 113 | private location: PlatformLocation, |
e945b184 | 114 | @Inject(LOCALE_ID) private localeId: string |
2666fd7c | 115 | ) { } |
dc8bc31b | 116 | |
b2731bff C |
117 | get user () { |
118 | return this.authService.getUser() | |
119 | } | |
120 | ||
d3217560 RK |
121 | get anonymousUser () { |
122 | return this.userService.getAnonymousUser() | |
123 | } | |
124 | ||
98ab5dc8 | 125 | ngOnInit () { |
6ebdd12f C |
126 | this.serverConfig = this.serverService.getHTMLConfig() |
127 | ||
1a568b6f C |
128 | PeertubePlayerManager.initState() |
129 | ||
c894a1ea C |
130 | this.loadRouteParams() |
131 | this.loadRouteQuery() | |
20d21199 | 132 | |
1c8ddbfa | 133 | this.initHotkeys() |
011e1e6b C |
134 | |
135 | this.theaterEnabled = getStoredTheater() | |
18a6f04c | 136 | |
c9e3eeed | 137 | this.hooks.runAction('action:video-watch.init', 'video-watch') |
58b9ce30 | 138 | |
139 | setTimeout(cleanupVideoWatch, 1500) // Run in timeout to ensure we're not blocking the UI | |
d1992b93 C |
140 | } |
141 | ||
df98563e | 142 | ngOnDestroy () { |
09edde40 | 143 | this.flushPlayer() |
067e3f84 | 144 | |
13fc89f4 | 145 | // Unsubscribe subscriptions |
e2f01c47 C |
146 | if (this.paramsSub) this.paramsSub.unsubscribe() |
147 | if (this.queryParamsSub) this.queryParamsSub.unsubscribe() | |
5abc96fc | 148 | if (this.configSub) this.configSub.unsubscribe() |
a5cf76af | 149 | if (this.liveVideosSub) this.liveVideosSub.unsubscribe() |
20d21199 RK |
150 | |
151 | // Unbind hotkeys | |
3d216ea0 | 152 | this.hotkeysService.remove(this.hotkeys) |
dc8bc31b | 153 | } |
98b01bac | 154 | |
06a55579 C |
155 | getCurrentTime () { |
156 | return this.currentTime | |
6863f814 RK |
157 | } |
158 | ||
06a55579 C |
159 | getCurrentPlaylistPosition () { |
160 | return this.videoWatchPlaylist.currentPlaylistPosition | |
99cc4f49 C |
161 | } |
162 | ||
6aa54148 | 163 | onRecommendations (videos: Video[]) { |
c894a1ea | 164 | if (videos.length === 0) return |
6aa54148 | 165 | |
c894a1ea C |
166 | // The recommended videos's first element should be the next video |
167 | const video = videos[0] | |
168 | this.nextVideoUUID = video.uuid | |
169 | this.nextVideoTitle = video.name | |
b29bf61d RK |
170 | } |
171 | ||
172 | handleTimestampClicked (timestamp: number) { | |
18429d01 C |
173 | if (!this.player || this.video.isLive) return |
174 | ||
175 | this.player.currentTime(timestamp) | |
b29bf61d | 176 | scrollToTop() |
706c5a47 RK |
177 | } |
178 | ||
c894a1ea C |
179 | onPlaylistVideoFound (videoId: string) { |
180 | this.loadVideo(videoId) | |
181 | } | |
182 | ||
5a2f775a C |
183 | onPlaylistNoVideoFound () { |
184 | this.noPlaylistVideoFound = true | |
185 | } | |
186 | ||
c894a1ea C |
187 | isUserLoggedIn () { |
188 | return this.authService.isLoggedIn() | |
189 | } | |
190 | ||
191 | isVideoBlur (video: Video) { | |
192 | return video.isVideoNSFWForUser(this.user, this.serverConfig) | |
706c5a47 RK |
193 | } |
194 | ||
b40a2193 K |
195 | isChannelDisplayNameGeneric () { |
196 | const genericChannelDisplayName = [ | |
197 | `Main ${this.video.channel.ownerAccount.name} channel`, | |
198 | `Default ${this.video.channel.ownerAccount.name} channel` | |
199 | ] | |
200 | ||
201 | return genericChannelDisplayName.includes(this.video.channel.displayName) | |
202 | } | |
203 | ||
0f7407d9 C |
204 | displayOtherVideosAsRow () { |
205 | // Use the same value as in the SASS file | |
206 | return this.screenService.getWindowInnerWidth() <= 1100 | |
207 | } | |
208 | ||
c894a1ea C |
209 | private loadRouteParams () { |
210 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
9df52d66 | 211 | const videoId = routeParams['videoId'] |
c894a1ea C |
212 | if (videoId) return this.loadVideo(videoId) |
213 | ||
9df52d66 | 214 | const playlistId = routeParams['playlistId'] |
c894a1ea C |
215 | if (playlistId) return this.loadPlaylist(playlistId) |
216 | }) | |
217 | } | |
218 | ||
219 | private loadRouteQuery () { | |
220 | this.queryParamsSub = this.route.queryParams.subscribe(queryParams => { | |
221 | // Handle the ?playlistPosition | |
9df52d66 | 222 | const positionParam = queryParams['playlistPosition'] ?? 1 |
c894a1ea C |
223 | |
224 | this.playlistPosition = positionParam === 'last' | |
225 | ? -1 // Handle the "last" index | |
226 | : parseInt(positionParam + '', 10) | |
227 | ||
228 | if (isNaN(this.playlistPosition)) { | |
42b40636 | 229 | logger.error(`playlistPosition query param '${positionParam}' was parsed as NaN, defaulting to 1.`) |
c894a1ea C |
230 | this.playlistPosition = 1 |
231 | } | |
232 | ||
233 | this.videoWatchPlaylist.updatePlaylistIndex(this.playlistPosition) | |
234 | ||
9df52d66 | 235 | const start = queryParams['start'] |
c894a1ea C |
236 | if (this.player && start) this.player.currentTime(parseInt(start, 10)) |
237 | }) | |
238 | } | |
239 | ||
e2f01c47 | 240 | private loadVideo (videoId: string) { |
c894a1ea | 241 | if (this.isSameElement(this.video, videoId)) return |
e2f01c47 C |
242 | |
243 | if (this.player) this.player.pause() | |
244 | ||
5a9a56b7 C |
245 | this.video = undefined |
246 | ||
93cae479 C |
247 | const videoObs = this.hooks.wrapObsFun( |
248 | this.videoService.getVideo.bind(this.videoService), | |
249 | { videoId }, | |
250 | 'video-watch', | |
251 | 'filter:api.video-watch.video.get.params', | |
252 | 'filter:api.video-watch.video.get.result' | |
253 | ) | |
254 | ||
f443a746 C |
255 | const videoAndLiveObs: Observable<{ video: VideoDetails, live?: LiveVideo }> = videoObs.pipe( |
256 | switchMap(video => { | |
257 | if (!video.isLive) return of({ video }) | |
258 | ||
259 | return this.liveVideoService.getVideoLive(video.uuid) | |
260 | .pipe(map(live => ({ live, video }))) | |
261 | }) | |
262 | ) | |
263 | ||
a9bfa85d | 264 | forkJoin([ |
f443a746 | 265 | videoAndLiveObs, |
a9bfa85d C |
266 | this.videoCaptionService.listCaptions(videoId), |
267 | this.userService.getAnonymousOrLoggedUser() | |
268 | ]).subscribe({ | |
f443a746 | 269 | next: ([ { video, live }, captionsResult, loggedInOrAnonymousUser ]) => { |
a9bfa85d | 270 | const queryParams = this.route.snapshot.queryParams |
e6abf95e | 271 | |
a9bfa85d C |
272 | const urlOptions = { |
273 | resume: queryParams.resume, | |
e2f01c47 | 274 | |
a9bfa85d C |
275 | startTime: queryParams.start, |
276 | stopTime: queryParams.stop, | |
3c6a44a1 | 277 | |
a9bfa85d C |
278 | muted: queryParams.muted, |
279 | loop: queryParams.loop, | |
280 | subtitle: queryParams.subtitle, | |
5efab546 | 281 | |
a9bfa85d C |
282 | playerMode: queryParams.mode, |
283 | peertubeLink: false | |
284 | } | |
5efab546 | 285 | |
f443a746 | 286 | this.onVideoFetched({ video, live, videoCaptions: captionsResult.data, loggedInOrAnonymousUser, urlOptions }) |
a9bfa85d C |
287 | .catch(err => this.handleGlobalError(err)) |
288 | }, | |
4c72c1cd | 289 | |
a9bfa85d C |
290 | error: err => this.handleRequestError(err) |
291 | }) | |
e2f01c47 C |
292 | } |
293 | ||
294 | private loadPlaylist (playlistId: string) { | |
c894a1ea | 295 | if (this.isSameElement(this.playlist, playlistId)) return |
e2f01c47 | 296 | |
5a2f775a C |
297 | this.noPlaylistVideoFound = false |
298 | ||
e2f01c47 | 299 | this.playlistService.getVideoPlaylist(playlistId) |
1378c0d3 C |
300 | .subscribe({ |
301 | next: playlist => { | |
c894a1ea C |
302 | this.playlist = playlist |
303 | ||
304 | this.videoWatchPlaylist.loadPlaylistElements(playlist, !this.playlistPosition, this.playlistPosition) | |
305 | }, | |
306 | ||
1378c0d3 C |
307 | error: err => this.handleRequestError(err) |
308 | }) | |
c894a1ea | 309 | } |
e2f01c47 | 310 | |
c894a1ea C |
311 | private isSameElement (element: VideoDetails | VideoPlaylist, newId: string) { |
312 | if (!element) return false | |
313 | ||
314 | return (element.id + '') === newId || element.uuid === newId || element.shortUUID === newId | |
315 | } | |
316 | ||
317 | private async handleRequestError (err: any) { | |
318 | const errorBody = err.body as PeerTubeProblemDocument | |
319 | ||
231ff4af | 320 | if (errorBody?.code === ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS && errorBody.originUrl) { |
c894a1ea C |
321 | const originUrl = errorBody.originUrl + (window.location.search ?? '') |
322 | ||
323 | const res = await this.confirmService.confirm( | |
9df52d66 | 324 | // eslint-disable-next-line max-len |
c894a1ea C |
325 | $localize`This video is not available on this instance. Do you want to be redirected on the origin instance: <a href="${originUrl}">${originUrl}</a>?`, |
326 | $localize`Redirection` | |
327 | ) | |
328 | ||
329 | if (res === true) return window.location.href = originUrl | |
330 | } | |
331 | ||
332 | // If 400, 403 or 404, the video is private or blocked so redirect to 404 | |
333 | return this.restExtractor.redirectTo404IfNotFound(err, 'video', [ | |
334 | HttpStatusCode.BAD_REQUEST_400, | |
335 | HttpStatusCode.FORBIDDEN_403, | |
336 | HttpStatusCode.NOT_FOUND_404 | |
337 | ]) | |
e2f01c47 C |
338 | } |
339 | ||
c894a1ea | 340 | private handleGlobalError (err: any) { |
0c31c33d | 341 | const errorMessage: string = typeof err === 'string' ? err : err.message |
bf5685f0 C |
342 | if (!errorMessage) return |
343 | ||
6d88de72 | 344 | // Display a message in the video player instead of a notification |
9df52d66 | 345 | if (errorMessage.includes('from xs param')) { |
6d88de72 C |
346 | this.flushPlayer() |
347 | this.remoteServerDown = true | |
3b492bff | 348 | |
6d88de72 | 349 | return |
0c31c33d C |
350 | } |
351 | ||
f8b2c1b4 | 352 | this.notifier.error(errorMessage) |
0c31c33d C |
353 | } |
354 | ||
a9bfa85d C |
355 | private async onVideoFetched (options: { |
356 | video: VideoDetails | |
f443a746 | 357 | live: LiveVideo |
a9bfa85d | 358 | videoCaptions: VideoCaption[] |
a5cf76af | 359 | urlOptions: URLOptions |
a9bfa85d C |
360 | loggedInOrAnonymousUser: User |
361 | }) { | |
f443a746 | 362 | const { video, live, videoCaptions, urlOptions, loggedInOrAnonymousUser } = options |
a9bfa85d | 363 | |
a5cf76af C |
364 | this.subscribeToLiveEventsIfNeeded(this.video, video) |
365 | ||
df98563e | 366 | this.video = video |
2f4c784a | 367 | this.videoCaptions = videoCaptions |
f443a746 | 368 | this.liveVideo = live |
92fb909c | 369 | |
c448d412 | 370 | // Re init attributes |
c15d61f5 | 371 | this.playerPlaceholderImgSrc = undefined |
6d88de72 | 372 | this.remoteServerDown = false |
f0a39880 | 373 | this.currentTime = undefined |
c448d412 | 374 | |
e2f01c47 | 375 | if (this.isVideoBlur(this.video)) { |
22b59e80 | 376 | const res = await this.confirmService.confirm( |
66357162 C |
377 | $localize`This video contains mature or explicit content. Are you sure you want to watch it?`, |
378 | $localize`Mature or explicit content` | |
d6e32a2e | 379 | ) |
60c2bc80 | 380 | if (res === false) return this.location.back() |
92fb909c C |
381 | } |
382 | ||
a9bfa85d | 383 | this.buildPlayer(urlOptions, loggedInOrAnonymousUser) |
42b40636 | 384 | .catch(err => logger.error('Cannot build the player', err)) |
0a6817f0 | 385 | |
0a6817f0 | 386 | this.setOpenGraphTags() |
0a6817f0 | 387 | |
55b84d53 C |
388 | const hookOptions = { |
389 | videojs, | |
390 | video: this.video, | |
391 | playlist: this.playlist | |
392 | } | |
393 | this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', hookOptions) | |
0a6817f0 C |
394 | } |
395 | ||
a9bfa85d | 396 | private async buildPlayer (urlOptions: URLOptions, loggedInOrAnonymousUser: User) { |
09edde40 C |
397 | // Flush old player if needed |
398 | this.flushPlayer() | |
b891f9bc | 399 | |
c15d61f5 C |
400 | const videoState = this.video.state.id |
401 | if (videoState === VideoState.LIVE_ENDED || videoState === VideoState.WAITING_FOR_LIVE) { | |
402 | this.playerPlaceholderImgSrc = this.video.previewPath | |
403 | return | |
404 | } | |
405 | ||
60c2bc80 | 406 | // Build video element, because videojs removes it on dispose |
e2f01c47 | 407 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper') |
b891f9bc C |
408 | this.playerElement = document.createElement('video') |
409 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 410 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
411 | playerElementWrapper.appendChild(this.playerElement) |
412 | ||
3d9a63d3 C |
413 | const params = { |
414 | video: this.video, | |
0a6817f0 | 415 | videoCaptions: this.videoCaptions, |
f443a746 | 416 | liveVideo: this.liveVideo, |
3d9a63d3 | 417 | urlOptions, |
a9bfa85d | 418 | loggedInOrAnonymousUser, |
3d9a63d3 | 419 | user: this.user |
e945b184 | 420 | } |
3d9a63d3 C |
421 | const { playerMode, playerOptions } = await this.hooks.wrapFun( |
422 | this.buildPlayerManagerOptions.bind(this), | |
423 | params, | |
c2023a9f C |
424 | 'video-watch', |
425 | 'filter:internal.video-watch.player.build-options.params', | |
3d9a63d3 C |
426 | 'filter:internal.video-watch.player.build-options.result' |
427 | ) | |
e945b184 | 428 | |
e945b184 | 429 | this.zone.runOutsideAngular(async () => { |
3d9a63d3 | 430 | this.player = await PeertubePlayerManager.initialize(playerMode, playerOptions, player => this.player = player) |
9a18a625 | 431 | |
57d65032 C |
432 | this.player.on('customError', (_e, data: any) => { |
433 | this.zone.run(() => this.handleGlobalError(data.err)) | |
c894a1ea | 434 | }) |
f0a39880 C |
435 | |
436 | this.player.on('timeupdate', () => { | |
c894a1ea | 437 | // Don't need to trigger angular change for this variable, that is sent to children components on click |
f0a39880 C |
438 | this.currentTime = Math.floor(this.player.currentTime()) |
439 | }) | |
e2f01c47 | 440 | |
3bcb4fd7 | 441 | /** |
c894a1ea C |
442 | * condition: true to make the upnext functionality trigger, false to disable the upnext functionality |
443 | * go to the next video in 'condition()' if you don't want of the timer. | |
444 | * next: function triggered at the end of the timer. | |
445 | * suspended: function used at each click of the timer checking if we need to reset progress | |
446 | * and wait until suspended becomes truthy again. | |
3bcb4fd7 RK |
447 | */ |
448 | this.player.upnext({ | |
c894a1ea C |
449 | timeout: 5000, // 5s |
450 | ||
66357162 C |
451 | headText: $localize`Up Next`, |
452 | cancelText: $localize`Cancel`, | |
453 | suspendedText: $localize`Autoplay is suspended`, | |
c894a1ea | 454 | |
3bcb4fd7 | 455 | getTitle: () => this.nextVideoTitle, |
c894a1ea C |
456 | |
457 | next: () => this.zone.run(() => this.playNextVideoInAngularZone()), | |
3bcb4fd7 | 458 | condition: () => { |
c894a1ea C |
459 | if (!this.playlist) return this.isAutoPlayNext() |
460 | ||
461 | // Don't wait timeout to play the next playlist video | |
462 | if (this.isPlaylistAutoPlayNext()) { | |
463 | this.playNextVideoInAngularZone() | |
464 | return undefined | |
3bcb4fd7 | 465 | } |
c894a1ea C |
466 | |
467 | return false | |
223b24e6 | 468 | }, |
c894a1ea | 469 | |
223b24e6 RK |
470 | suspended: () => { |
471 | return ( | |
57d65032 | 472 | !isXPercentInViewport(this.player.el() as HTMLElement, 80) || |
223b24e6 RK |
473 | !document.getElementById('content').contains(document.activeElement) |
474 | ) | |
e2f01c47 C |
475 | } |
476 | }) | |
477 | ||
478 | this.player.one('stopped', () => { | |
c894a1ea C |
479 | if (this.playlist && this.isPlaylistAutoPlayNext()) { |
480 | this.playNextVideoInAngularZone() | |
e2f01c47 C |
481 | } |
482 | }) | |
9a18a625 | 483 | |
e772bdf1 C |
484 | this.player.one('ended', () => { |
485 | if (this.video.isLive) { | |
ceb8f322 | 486 | this.zone.run(() => this.video.state.id = VideoState.LIVE_ENDED) |
e772bdf1 C |
487 | } |
488 | }) | |
489 | ||
9a18a625 C |
490 | this.player.on('theaterChange', (_: any, enabled: boolean) => { |
491 | this.zone.run(() => this.theaterEnabled = enabled) | |
492 | }) | |
5f85f8aa | 493 | |
c3bb0441 | 494 | this.hooks.runAction('action:video-watch.player.loaded', 'video-watch', { |
495 | player: this.player, | |
496 | playlist: this.playlist, | |
497 | playlistPosition: this.playlistPosition, | |
498 | videojs, | |
499 | video: this.video | |
500 | }) | |
b891f9bc | 501 | }) |
92fb909c C |
502 | } |
503 | ||
a5a79d15 C |
504 | private hasNextVideo () { |
505 | if (this.playlist) { | |
506 | return this.videoWatchPlaylist.hasNextVideo() | |
507 | } | |
508 | ||
509 | return true | |
510 | } | |
511 | ||
c894a1ea | 512 | private playNextVideoInAngularZone () { |
6dd873d6 RK |
513 | if (this.playlist) { |
514 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) | |
c894a1ea | 515 | return |
6aa54148 | 516 | } |
3ec343a4 | 517 | |
c894a1ea C |
518 | if (this.nextVideoUUID) { |
519 | this.router.navigate([ '/w', this.nextVideoUUID ]) | |
c894a1ea | 520 | } |
3ec343a4 | 521 | } |
1f3e9fec | 522 | |
d4c6a3b9 | 523 | private isAutoplay () { |
bf079b7b C |
524 | // We'll jump to the thread id, so do not play the video |
525 | if (this.route.snapshot.params['threadId']) return false | |
526 | ||
527 | // Otherwise true by default | |
d4c6a3b9 C |
528 | if (!this.user) return true |
529 | ||
530 | // Be sure the autoPlay is set to false | |
531 | return this.user.autoPlayVideo !== false | |
532 | } | |
09edde40 | 533 | |
c894a1ea C |
534 | private isAutoPlayNext () { |
535 | return ( | |
9df52d66 | 536 | (this.user?.autoPlayNextVideo) || |
c894a1ea C |
537 | this.anonymousUser.autoPlayNextVideo |
538 | ) | |
539 | } | |
540 | ||
541 | private isPlaylistAutoPlayNext () { | |
542 | return ( | |
9df52d66 | 543 | (this.user?.autoPlayNextVideoPlaylist) || |
c894a1ea C |
544 | this.anonymousUser.autoPlayNextVideoPlaylist |
545 | ) | |
546 | } | |
547 | ||
09edde40 C |
548 | private flushPlayer () { |
549 | // Remove player if it exists | |
d4a8e7a6 C |
550 | if (!this.player) return |
551 | ||
552 | try { | |
553 | this.player.dispose() | |
554 | this.player = undefined | |
555 | } catch (err) { | |
42b40636 | 556 | logger.error('Cannot dispose player.', err) |
09edde40 C |
557 | } |
558 | } | |
1c8ddbfa | 559 | |
3d9a63d3 | 560 | private buildPlayerManagerOptions (params: { |
9df52d66 | 561 | video: VideoDetails |
f443a746 | 562 | liveVideo: LiveVideo |
9df52d66 C |
563 | videoCaptions: VideoCaption[] |
564 | urlOptions: CustomizationOptions & { playerMode: PlayerMode } | |
a9bfa85d | 565 | loggedInOrAnonymousUser: User |
384ba8b7 | 566 | user?: AuthUser // Keep for plugins |
3d9a63d3 | 567 | }) { |
384ba8b7 | 568 | const { video, liveVideo, videoCaptions, urlOptions, loggedInOrAnonymousUser } = params |
c894a1ea | 569 | |
706c5a47 RK |
570 | const getStartTime = () => { |
571 | const byUrl = urlOptions.startTime !== undefined | |
96f6278f | 572 | const byHistory = video.userHistory && (!this.playlist || urlOptions.resume !== undefined) |
58b9ce30 | 573 | const byLocalStorage = getStoredVideoWatchHistory(video.uuid) |
706c5a47 | 574 | |
3c6a44a1 C |
575 | if (byUrl) return timeToInt(urlOptions.startTime) |
576 | if (byHistory) return video.userHistory.currentTime | |
58b9ce30 | 577 | if (byLocalStorage) return byLocalStorage.duration |
3c6a44a1 C |
578 | |
579 | return 0 | |
706c5a47 | 580 | } |
3d9a63d3 | 581 | |
706c5a47 | 582 | let startTime = getStartTime() |
3c6a44a1 | 583 | |
3d9a63d3 C |
584 | // If we are at the end of the video, reset the timer |
585 | if (video.duration - startTime <= 1) startTime = 0 | |
586 | ||
587 | const playerCaptions = videoCaptions.map(c => ({ | |
588 | label: c.language.label, | |
589 | language: c.language.id, | |
590 | src: environment.apiUrl + c.captionPath | |
591 | })) | |
592 | ||
f443a746 C |
593 | const liveOptions = video.isLive |
594 | ? { latencyMode: liveVideo.latencyMode } | |
595 | : undefined | |
596 | ||
3d9a63d3 C |
597 | const options: PeertubePlayerManagerOptions = { |
598 | common: { | |
599 | autoplay: this.isAutoplay(), | |
a9bfa85d C |
600 | p2pEnabled: isP2PEnabled(video, this.serverConfig, loggedInOrAnonymousUser.p2pEnabled), |
601 | ||
a5a79d15 | 602 | hasNextVideo: () => this.hasNextVideo(), |
c894a1ea | 603 | nextVideo: () => this.playNextVideoInAngularZone(), |
3d9a63d3 C |
604 | |
605 | playerElement: this.playerElement, | |
606 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, | |
607 | ||
608 | videoDuration: video.duration, | |
609 | enableHotkeys: true, | |
610 | inactivityTimeout: 2500, | |
611 | poster: video.previewUrl, | |
612 | ||
613 | startTime, | |
614 | stopTime: urlOptions.stopTime, | |
60f013e1 | 615 | controlBar: urlOptions.controlBar, |
3d9a63d3 C |
616 | controls: urlOptions.controls, |
617 | muted: urlOptions.muted, | |
618 | loop: urlOptions.loop, | |
619 | subtitle: urlOptions.subtitle, | |
620 | ||
621 | peertubeLink: urlOptions.peertubeLink, | |
622 | ||
623 | theaterButton: true, | |
624 | captions: videoCaptions.length !== 0, | |
625 | ||
626 | videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE | |
627 | ? this.videoService.getVideoViewUrl(video.uuid) | |
628 | : null, | |
384ba8b7 C |
629 | authorizationHeader: this.authService.getRequestHeaderValue(), |
630 | ||
3d9a63d3 | 631 | embedUrl: video.embedUrl, |
4097c6d6 | 632 | embedTitle: video.name, |
bd2b51be | 633 | instanceName: this.serverConfig.instance.name, |
3d9a63d3 | 634 | |
25b7c847 | 635 | isLive: video.isLive, |
f443a746 | 636 | liveOptions, |
25b7c847 | 637 | |
3d9a63d3 C |
638 | language: this.localeId, |
639 | ||
3d9a63d3 C |
640 | serverUrl: environment.apiUrl, |
641 | ||
58b9ce30 | 642 | videoCaptions: playerCaptions, |
643 | ||
9162fdd3 | 644 | videoShortUUID: video.shortUUID, |
c4207f97 C |
645 | videoUUID: video.uuid, |
646 | ||
647 | errorNotifier: (message: string) => this.notifier.error(message) | |
3d9a63d3 C |
648 | }, |
649 | ||
650 | webtorrent: { | |
651 | videoFiles: video.files | |
72f611ca | 652 | }, |
653 | ||
654 | pluginsManager: this.pluginService.getPluginsManager() | |
3d9a63d3 C |
655 | } |
656 | ||
dfdcbb94 | 657 | // Only set this if we're in a playlist |
5bb2ed6b | 658 | if (this.playlist) { |
a5a79d15 C |
659 | options.common.hasPreviousVideo = () => this.videoWatchPlaylist.hasPreviousVideo() |
660 | ||
5bb2ed6b P |
661 | options.common.previousVideo = () => { |
662 | this.zone.run(() => this.videoWatchPlaylist.navigateToPreviousPlaylistVideo()) | |
663 | } | |
dfdcbb94 P |
664 | } |
665 | ||
3d9a63d3 C |
666 | let mode: PlayerMode |
667 | ||
668 | if (urlOptions.playerMode) { | |
669 | if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader' | |
670 | else mode = 'webtorrent' | |
671 | } else { | |
672 | if (video.hasHlsPlaylist()) mode = 'p2p-media-loader' | |
673 | else mode = 'webtorrent' | |
674 | } | |
675 | ||
c894a1ea | 676 | // p2p-media-loader needs TextEncoder, fallback on WebTorrent if not available |
089af69b C |
677 | if (typeof TextEncoder === 'undefined') { |
678 | mode = 'webtorrent' | |
679 | } | |
680 | ||
3d9a63d3 C |
681 | if (mode === 'p2p-media-loader') { |
682 | const hlsPlaylist = video.getHlsPlaylist() | |
683 | ||
684 | const p2pMediaLoader = { | |
685 | playlistUrl: hlsPlaylist.playlistUrl, | |
686 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
687 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
688 | trackerAnnounce: video.trackerUrls, | |
689 | videoFiles: hlsPlaylist.files | |
690 | } as P2PMediaLoaderOptions | |
691 | ||
692 | Object.assign(options, { p2pMediaLoader }) | |
693 | } | |
694 | ||
695 | return { playerMode: mode, playerOptions: options } | |
696 | } | |
697 | ||
a5cf76af C |
698 | private async subscribeToLiveEventsIfNeeded (oldVideo: VideoDetails, newVideo: VideoDetails) { |
699 | if (!this.liveVideosSub) { | |
a800dbf3 | 700 | this.liveVideosSub = this.buildLiveEventsSubscription() |
a5cf76af C |
701 | } |
702 | ||
703 | if (oldVideo && oldVideo.id !== newVideo.id) { | |
98ab5dc8 | 704 | this.peertubeSocket.unsubscribeLiveVideos(oldVideo.id) |
a5cf76af C |
705 | } |
706 | ||
707 | if (!newVideo.isLive) return | |
708 | ||
709 | await this.peertubeSocket.subscribeToLiveVideosSocket(newVideo.id) | |
710 | } | |
711 | ||
a800dbf3 C |
712 | private buildLiveEventsSubscription () { |
713 | return this.peertubeSocket.getLiveVideosObservable() | |
714 | .subscribe(({ type, payload }) => { | |
715 | if (type === 'state-change') return this.handleLiveStateChange(payload.state) | |
51353d9a | 716 | if (type === 'views-change') return this.handleLiveViewsChange(payload.viewers) |
a800dbf3 C |
717 | }) |
718 | } | |
719 | ||
720 | private handleLiveStateChange (newState: VideoState) { | |
721 | if (newState !== VideoState.PUBLISHED) return | |
722 | ||
42b40636 | 723 | logger.info('Loading video after live update.') |
a800dbf3 C |
724 | |
725 | const videoUUID = this.video.uuid | |
726 | ||
c894a1ea | 727 | // Reset to force refresh the video |
a800dbf3 C |
728 | this.video = undefined |
729 | this.loadVideo(videoUUID) | |
730 | } | |
731 | ||
51353d9a | 732 | private handleLiveViewsChange (newViewers: number) { |
a800dbf3 | 733 | if (!this.video) { |
42b40636 | 734 | logger.error('Cannot update video live views because video is no defined.') |
a800dbf3 C |
735 | return |
736 | } | |
737 | ||
42b40636 | 738 | logger.info('Updating live views.') |
e43b5a3f | 739 | |
51353d9a | 740 | this.video.viewers = newViewers |
a800dbf3 C |
741 | } |
742 | ||
941c5eac C |
743 | private initHotkeys () { |
744 | this.hotkeys = [ | |
941c5eac | 745 | // These hotkeys are managed by the player |
fc3412fd C |
746 | new Hotkey('f', e => e, undefined, $localize`Enter/exit fullscreen`), |
747 | new Hotkey('space', e => e, undefined, $localize`Play/Pause the video`), | |
748 | new Hotkey('m', e => e, undefined, $localize`Mute/unmute the video`), | |
941c5eac | 749 | |
fc3412fd | 750 | new Hotkey('0-9', e => e, undefined, $localize`Skip to a percentage of the video: 0 is 0% and 9 is 90%`), |
941c5eac | 751 | |
fc3412fd C |
752 | new Hotkey('up', e => e, undefined, $localize`Increase the volume`), |
753 | new Hotkey('down', e => e, undefined, $localize`Decrease the volume`), | |
941c5eac | 754 | |
fc3412fd C |
755 | new Hotkey('right', e => e, undefined, $localize`Seek the video forward`), |
756 | new Hotkey('left', e => e, undefined, $localize`Seek the video backward`), | |
941c5eac | 757 | |
fc3412fd C |
758 | new Hotkey('>', e => e, undefined, $localize`Increase playback rate`), |
759 | new Hotkey('<', e => e, undefined, $localize`Decrease playback rate`), | |
941c5eac | 760 | |
fc3412fd C |
761 | new Hotkey(',', e => e, undefined, $localize`Navigate in the video to the previous frame`), |
762 | new Hotkey('.', e => e, undefined, $localize`Navigate in the video to the next frame`), | |
763 | ||
764 | new Hotkey('t', e => { | |
765 | this.theaterEnabled = !this.theaterEnabled | |
766 | return false | |
767 | }, undefined, $localize`Toggle theater mode`) | |
941c5eac | 768 | ] |
3d216ea0 C |
769 | |
770 | if (this.isUserLoggedIn()) { | |
771 | this.hotkeys = this.hotkeys.concat([ | |
3d216ea0 | 772 | new Hotkey('shift+s', () => { |
9df52d66 C |
773 | if (this.subscribeButton.isSubscribedToAll()) this.subscribeButton.unsubscribe() |
774 | else this.subscribeButton.subscribe() | |
77d873c5 | 775 | |
3d216ea0 | 776 | return false |
66357162 | 777 | }, undefined, $localize`Subscribe to the account`) |
3d216ea0 C |
778 | ]) |
779 | } | |
780 | ||
781 | this.hotkeysService.add(this.hotkeys) | |
941c5eac | 782 | } |
c894a1ea C |
783 | |
784 | private setOpenGraphTags () { | |
785 | this.metaService.setTitle(this.video.name) | |
786 | ||
787 | this.metaService.setTag('og:type', 'video') | |
788 | ||
789 | this.metaService.setTag('og:title', this.video.name) | |
790 | this.metaService.setTag('name', this.video.name) | |
791 | ||
792 | this.metaService.setTag('og:description', this.video.description) | |
793 | this.metaService.setTag('description', this.video.description) | |
794 | ||
795 | this.metaService.setTag('og:image', this.video.previewPath) | |
796 | ||
797 | this.metaService.setTag('og:duration', this.video.duration.toString()) | |
798 | ||
799 | this.metaService.setTag('og:site_name', 'PeerTube') | |
800 | ||
801 | this.metaService.setTag('og:url', window.location.href) | |
802 | this.metaService.setTag('url', window.location.href) | |
803 | } | |
dc8bc31b | 804 | } |