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