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