]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
2 | import { forkJoin, Observable, Subscription } from 'rxjs' | |
e972e046 | 3 | import { catchError } from 'rxjs/operators' |
67ed6552 | 4 | import { PlatformLocation } from '@angular/common' |
3b492bff | 5 | import { ChangeDetectorRef, 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, | |
11 | MarkdownService, | |
12 | Notifier, | |
13 | PeerTubeSocket, | |
14 | RestExtractor, | |
2666fd7c | 15 | ScreenService, |
a5cf76af C |
16 | ServerService, |
17 | UserService | |
18 | } from '@app/core' | |
67ed6552 | 19 | import { HooksService } from '@app/core/plugins/hooks.service' |
901637bb | 20 | import { RedirectService } from '@app/core/routing/redirect.service' |
4504f09f | 21 | import { isXPercentInViewport, scrollToTop } from '@app/helpers' |
67ed6552 | 22 | import { Video, VideoCaptionService, VideoDetails, VideoService } from '@app/shared/shared-main' |
82f443de | 23 | import { VideoShareComponent } from '@app/shared/shared-share-modal' |
100d9ce2 | 24 | import { SupportModalComponent } from '@app/shared/shared-support-modal' |
67ed6552 | 25 | import { SubscribeButtonComponent } from '@app/shared/shared-user-subscription' |
f8c00564 | 26 | import { VideoActionsDisplayType, VideoDownloadComponent } from '@app/shared/shared-video-miniature' |
67ed6552 | 27 | import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist' |
1f3e9fec | 28 | import { MetaService } from '@ngx-meta/core' |
82f443de | 29 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' |
a800dbf3 | 30 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
e6abf95e | 31 | import { ServerConfig, ServerErrorCode, UserVideoRateType, VideoCaption, VideoPrivacy, VideoState } from '@shared/models' |
cdeddff1 C |
32 | import { |
33 | cleanupVideoWatch, | |
34 | getStoredP2PEnabled, | |
35 | getStoredTheater, | |
36 | getStoredVideoWatchHistory | |
37 | } from '../../../assets/player/peertube-player-local-storage' | |
6ec0b75b | 38 | import { |
5efab546 | 39 | CustomizationOptions, |
6ec0b75b C |
40 | P2PMediaLoaderOptions, |
41 | PeertubePlayerManager, | |
42 | PeertubePlayerManagerOptions, | |
67ed6552 C |
43 | PlayerMode, |
44 | videojs | |
6ec0b75b | 45 | } from '../../../assets/player/peertube-player-manager' |
5efab546 | 46 | import { isWebRTCDisabled, timeToInt } from '../../../assets/player/utils' |
67ed6552 | 47 | import { environment } from '../../../environments/environment' |
67ed6552 | 48 | import { VideoWatchPlaylistComponent } from './video-watch-playlist.component' |
dc8bc31b | 49 | |
a5cf76af C |
50 | type URLOptions = CustomizationOptions & { playerMode: PlayerMode } |
51 | ||
dc8bc31b C |
52 | @Component({ |
53 | selector: 'my-video-watch', | |
ec8d8440 C |
54 | templateUrl: './video-watch.component.html', |
55 | styleUrls: [ './video-watch.component.scss' ] | |
dc8bc31b | 56 | }) |
0629423c | 57 | export class VideoWatchComponent implements OnInit, OnDestroy { |
22b59e80 C |
58 | private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern' |
59 | ||
f36da21e | 60 | @ViewChild('videoWatchPlaylist', { static: true }) videoWatchPlaylist: VideoWatchPlaylistComponent |
2f5d2ec5 | 61 | @ViewChild('videoShareModal') videoShareModal: VideoShareComponent |
100d9ce2 | 62 | @ViewChild('supportModal') supportModal: SupportModalComponent |
2f5d2ec5 | 63 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent |
6863f814 | 64 | @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent |
df98563e | 65 | |
2adfc7ea | 66 | player: any |
0826c92d | 67 | playerElement: HTMLVideoElement |
c15d61f5 | 68 | |
9a18a625 | 69 | theaterEnabled = false |
c15d61f5 | 70 | |
154898b0 | 71 | userRating: UserVideoRateType = null |
c15d61f5 C |
72 | |
73 | playerPlaceholderImgSrc: string | |
2de96f4d | 74 | |
2f4c784a C |
75 | video: VideoDetails = null |
76 | videoCaptions: VideoCaption[] = [] | |
77 | ||
d142c7b9 | 78 | playlistPosition: number |
e2f01c47 | 79 | playlist: VideoPlaylist = null |
e2f01c47 | 80 | |
c15d61f5 | 81 | descriptionLoading = false |
2de96f4d C |
82 | completeDescriptionShown = false |
83 | completeVideoDescription: string | |
84 | shortVideoDescription: string | |
9d9597df | 85 | videoHTMLDescription = '' |
c15d61f5 | 86 | |
e9189001 | 87 | likesBarTooltipText = '' |
c15d61f5 | 88 | |
73e09f27 | 89 | hasAlreadyAcceptedPrivacyConcern = false |
6d88de72 | 90 | remoteServerDown = false |
c15d61f5 | 91 | |
3d216ea0 | 92 | hotkeys: Hotkey[] = [] |
df98563e | 93 | |
94dfca3e RK |
94 | tooltipLike = '' |
95 | tooltipDislike = '' | |
96 | tooltipSupport = '' | |
97 | tooltipSaveToPlaylist = '' | |
98 | ||
f8c00564 C |
99 | videoActionsOptions: VideoActionsDisplayType = { |
100 | playlist: false, | |
101 | download: true, | |
102 | update: true, | |
103 | blacklist: true, | |
104 | delete: true, | |
105 | report: true, | |
106 | duplicate: true, | |
107 | mute: true, | |
108 | liveInfo: true | |
109 | } | |
110 | ||
6aa54148 | 111 | private nextVideoUuid = '' |
3bcb4fd7 | 112 | private nextVideoTitle = '' |
f0a39880 | 113 | private currentTime: number |
df98563e | 114 | private paramsSub: Subscription |
e2f01c47 | 115 | private queryParamsSub: Subscription |
31b6ddf8 | 116 | private configSub: Subscription |
a5cf76af | 117 | private liveVideosSub: Subscription |
df98563e | 118 | |
ba430d75 C |
119 | private serverConfig: ServerConfig |
120 | ||
df98563e | 121 | constructor ( |
4fd8aa32 | 122 | private elementRef: ElementRef, |
3b492bff | 123 | private changeDetector: ChangeDetectorRef, |
0629423c | 124 | private route: ActivatedRoute, |
92fb909c | 125 | private router: Router, |
d3ef341a | 126 | private videoService: VideoService, |
e2f01c47 | 127 | private playlistService: VideoPlaylistService, |
92fb909c | 128 | private confirmService: ConfirmService, |
3ec343a4 | 129 | private metaService: MetaService, |
7ddd02c9 | 130 | private authService: AuthService, |
d3217560 | 131 | private userService: UserService, |
0883b324 | 132 | private serverService: ServerService, |
a51bad1a | 133 | private restExtractor: RestExtractor, |
f8b2c1b4 | 134 | private notifier: Notifier, |
7ae71355 | 135 | private markdownService: MarkdownService, |
901637bb | 136 | private zone: NgZone, |
989e526a | 137 | private redirectService: RedirectService, |
16f7022b | 138 | private videoCaptionService: VideoCaptionService, |
20d21199 | 139 | private hotkeysService: HotkeysService, |
93cae479 | 140 | private hooks: HooksService, |
a5cf76af | 141 | private peertubeSocket: PeerTubeSocket, |
2666fd7c | 142 | private screenService: ScreenService, |
60c2bc80 | 143 | private location: PlatformLocation, |
e945b184 | 144 | @Inject(LOCALE_ID) private localeId: string |
2666fd7c | 145 | ) { } |
dc8bc31b | 146 | |
b2731bff C |
147 | get user () { |
148 | return this.authService.getUser() | |
149 | } | |
150 | ||
d3217560 RK |
151 | get anonymousUser () { |
152 | return this.userService.getAnonymousUser() | |
153 | } | |
154 | ||
18a6f04c | 155 | async ngOnInit () { |
2666fd7c C |
156 | // Hide the tooltips for unlogged users in mobile view, this adds confusion with the popover |
157 | if (this.user || !this.screenService.isInMobileView()) { | |
158 | this.tooltipLike = $localize`Like this video` | |
159 | this.tooltipDislike = $localize`Dislike this video` | |
160 | this.tooltipSupport = $localize`Support options for this video` | |
161 | this.tooltipSaveToPlaylist = $localize`Save to playlist` | |
162 | } | |
163 | ||
1a568b6f C |
164 | PeertubePlayerManager.initState() |
165 | ||
ba430d75 C |
166 | this.serverConfig = this.serverService.getTmpConfig() |
167 | ||
168 | this.configSub = this.serverService.getConfig() | |
169 | .subscribe(config => { | |
170 | this.serverConfig = config | |
171 | ||
31b6ddf8 C |
172 | if ( |
173 | isWebRTCDisabled() || | |
ba430d75 | 174 | this.serverConfig.tracker.enabled === false || |
c469c05b | 175 | getStoredP2PEnabled() === false || |
31b6ddf8 C |
176 | peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true' |
177 | ) { | |
178 | this.hasAlreadyAcceptedPrivacyConcern = true | |
179 | } | |
180 | }) | |
2b3b76ab | 181 | |
13fc89f4 | 182 | this.paramsSub = this.route.params.subscribe(routeParams => { |
e2f01c47 C |
183 | const videoId = routeParams[ 'videoId' ] |
184 | if (videoId) this.loadVideo(videoId) | |
a51bad1a | 185 | |
e2f01c47 C |
186 | const playlistId = routeParams[ 'playlistId' ] |
187 | if (playlistId) this.loadPlaylist(playlistId) | |
188 | }) | |
bf079b7b | 189 | |
d142c7b9 C |
190 | this.queryParamsSub = this.route.queryParams.subscribe(queryParams => { |
191 | this.playlistPosition = queryParams[ 'playlistPosition' ] | |
192 | this.videoWatchPlaylist.updatePlaylistIndex(this.playlistPosition) | |
b29bf61d RK |
193 | |
194 | const start = queryParams[ 'start' ] | |
195 | if (this.player && start) this.player.currentTime(parseInt(start, 10)) | |
df98563e | 196 | }) |
20d21199 | 197 | |
1c8ddbfa | 198 | this.initHotkeys() |
011e1e6b C |
199 | |
200 | this.theaterEnabled = getStoredTheater() | |
18a6f04c | 201 | |
c9e3eeed | 202 | this.hooks.runAction('action:video-watch.init', 'video-watch') |
58b9ce30 | 203 | |
204 | setTimeout(cleanupVideoWatch, 1500) // Run in timeout to ensure we're not blocking the UI | |
d1992b93 C |
205 | } |
206 | ||
df98563e | 207 | ngOnDestroy () { |
09edde40 | 208 | this.flushPlayer() |
067e3f84 | 209 | |
13fc89f4 | 210 | // Unsubscribe subscriptions |
e2f01c47 C |
211 | if (this.paramsSub) this.paramsSub.unsubscribe() |
212 | if (this.queryParamsSub) this.queryParamsSub.unsubscribe() | |
5abc96fc | 213 | if (this.configSub) this.configSub.unsubscribe() |
a5cf76af | 214 | if (this.liveVideosSub) this.liveVideosSub.unsubscribe() |
20d21199 RK |
215 | |
216 | // Unbind hotkeys | |
3d216ea0 | 217 | this.hotkeysService.remove(this.hotkeys) |
dc8bc31b | 218 | } |
98b01bac | 219 | |
df98563e C |
220 | setLike () { |
221 | if (this.isUserLoggedIn() === false) return | |
4c72c1cd C |
222 | |
223 | // Already liked this video | |
224 | if (this.userRating === 'like') this.setRating('none') | |
225 | else this.setRating('like') | |
d38b8281 C |
226 | } |
227 | ||
df98563e C |
228 | setDislike () { |
229 | if (this.isUserLoggedIn() === false) return | |
4c72c1cd C |
230 | |
231 | // Already disliked this video | |
232 | if (this.userRating === 'dislike') this.setRating('none') | |
233 | else this.setRating('dislike') | |
d38b8281 C |
234 | } |
235 | ||
0d3a9be9 C |
236 | getRatePopoverText () { |
237 | if (this.isUserLoggedIn()) return undefined | |
238 | ||
214ff6fa | 239 | return $localize`You need to be <a href="/login">logged in</a> to rate this video.` |
0d3a9be9 C |
240 | } |
241 | ||
2de96f4d | 242 | showMoreDescription () { |
2de96f4d C |
243 | if (this.completeVideoDescription === undefined) { |
244 | return this.loadCompleteDescription() | |
245 | } | |
246 | ||
247 | this.updateVideoDescription(this.completeVideoDescription) | |
80958c78 | 248 | this.completeDescriptionShown = true |
2de96f4d C |
249 | } |
250 | ||
251 | showLessDescription () { | |
2de96f4d | 252 | this.updateVideoDescription(this.shortVideoDescription) |
80958c78 | 253 | this.completeDescriptionShown = false |
2de96f4d C |
254 | } |
255 | ||
6863f814 RK |
256 | showDownloadModal () { |
257 | this.videoDownloadModal.show(this.video, this.videoCaptions) | |
258 | } | |
259 | ||
260 | isVideoDownloadable () { | |
d846d99c | 261 | return this.video && this.video instanceof VideoDetails && this.video.downloadEnabled && !this.video.isLive |
6863f814 RK |
262 | } |
263 | ||
2de96f4d | 264 | loadCompleteDescription () { |
80958c78 C |
265 | this.descriptionLoading = true |
266 | ||
2de96f4d | 267 | this.videoService.loadCompleteDescription(this.video.descriptionPath) |
2186386c C |
268 | .subscribe( |
269 | description => { | |
270 | this.completeDescriptionShown = true | |
271 | this.descriptionLoading = false | |
272 | ||
273 | this.shortVideoDescription = this.video.description | |
274 | this.completeVideoDescription = description | |
275 | ||
276 | this.updateVideoDescription(this.completeVideoDescription) | |
277 | }, | |
278 | ||
279 | error => { | |
280 | this.descriptionLoading = false | |
f8b2c1b4 | 281 | this.notifier.error(error.message) |
2186386c C |
282 | } |
283 | ) | |
2de96f4d C |
284 | } |
285 | ||
07fa4c97 | 286 | showSupportModal () { |
ca873292 | 287 | this.supportModal.show() |
07fa4c97 C |
288 | } |
289 | ||
df98563e | 290 | showShareModal () { |
951b582f | 291 | this.videoShareModal.show(this.currentTime, this.videoWatchPlaylist.currentPlaylistPosition) |
99cc4f49 C |
292 | } |
293 | ||
df98563e C |
294 | isUserLoggedIn () { |
295 | return this.authService.isLoggedIn() | |
4f8c0eb0 C |
296 | } |
297 | ||
b1fa3eba C |
298 | getVideoTags () { |
299 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
300 | ||
4278710d | 301 | return this.video.tags |
b1fa3eba C |
302 | } |
303 | ||
6aa54148 L |
304 | onRecommendations (videos: Video[]) { |
305 | if (videos.length > 0) { | |
3bcb4fd7 RK |
306 | // The recommended videos's first element should be the next video |
307 | const video = videos[0] | |
308 | this.nextVideoUuid = video.uuid | |
309 | this.nextVideoTitle = video.name | |
6aa54148 L |
310 | } |
311 | } | |
312 | ||
3a0fb65c C |
313 | onVideoRemoved () { |
314 | this.redirectService.redirectToHomepage() | |
6725d05c C |
315 | } |
316 | ||
d3217560 RK |
317 | declinedPrivacyConcern () { |
318 | peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'false') | |
319 | this.hasAlreadyAcceptedPrivacyConcern = false | |
320 | } | |
321 | ||
73e09f27 | 322 | acceptedPrivacyConcern () { |
0bd78bf3 | 323 | peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true') |
73e09f27 C |
324 | this.hasAlreadyAcceptedPrivacyConcern = true |
325 | } | |
326 | ||
2186386c C |
327 | isVideoToTranscode () { |
328 | return this.video && this.video.state.id === VideoState.TO_TRANSCODE | |
329 | } | |
330 | ||
516df59b C |
331 | isVideoToImport () { |
332 | return this.video && this.video.state.id === VideoState.TO_IMPORT | |
333 | } | |
334 | ||
bbe0f064 C |
335 | hasVideoScheduledPublication () { |
336 | return this.video && this.video.scheduledUpdate !== undefined | |
337 | } | |
338 | ||
a5cf76af C |
339 | isLive () { |
340 | return !!(this.video?.isLive) | |
341 | } | |
342 | ||
343 | isWaitingForLive () { | |
344 | return this.video?.state.id === VideoState.WAITING_FOR_LIVE | |
345 | } | |
346 | ||
347 | isLiveEnded () { | |
348 | return this.video?.state.id === VideoState.LIVE_ENDED | |
349 | } | |
350 | ||
e2f01c47 | 351 | isVideoBlur (video: Video) { |
ba430d75 | 352 | return video.isVideoNSFWForUser(this.user, this.serverConfig) |
e2f01c47 C |
353 | } |
354 | ||
706c5a47 RK |
355 | isAutoPlayEnabled () { |
356 | return ( | |
7c93905d | 357 | (this.user && this.user.autoPlayNextVideo) || |
d3217560 | 358 | this.anonymousUser.autoPlayNextVideo |
706c5a47 | 359 | ) |
b29bf61d RK |
360 | } |
361 | ||
362 | handleTimestampClicked (timestamp: number) { | |
18429d01 C |
363 | if (!this.player || this.video.isLive) return |
364 | ||
365 | this.player.currentTime(timestamp) | |
b29bf61d | 366 | scrollToTop() |
706c5a47 RK |
367 | } |
368 | ||
369 | isPlaylistAutoPlayEnabled () { | |
370 | return ( | |
7c93905d | 371 | (this.user && this.user.autoPlayNextVideoPlaylist) || |
d3217560 | 372 | this.anonymousUser.autoPlayNextVideoPlaylist |
706c5a47 RK |
373 | ) |
374 | } | |
375 | ||
b40a2193 K |
376 | isChannelDisplayNameGeneric () { |
377 | const genericChannelDisplayName = [ | |
378 | `Main ${this.video.channel.ownerAccount.name} channel`, | |
379 | `Default ${this.video.channel.ownerAccount.name} channel` | |
380 | ] | |
381 | ||
382 | return genericChannelDisplayName.includes(this.video.channel.displayName) | |
383 | } | |
384 | ||
d142c7b9 C |
385 | onPlaylistVideoFound (videoId: string) { |
386 | this.loadVideo(videoId) | |
387 | } | |
388 | ||
0f7407d9 C |
389 | displayOtherVideosAsRow () { |
390 | // Use the same value as in the SASS file | |
391 | return this.screenService.getWindowInnerWidth() <= 1100 | |
392 | } | |
393 | ||
e2f01c47 C |
394 | private loadVideo (videoId: string) { |
395 | // Video did not change | |
396 | if (this.video && this.video.uuid === videoId) return | |
397 | ||
398 | if (this.player) this.player.pause() | |
399 | ||
93cae479 C |
400 | const videoObs = this.hooks.wrapObsFun( |
401 | this.videoService.getVideo.bind(this.videoService), | |
402 | { videoId }, | |
403 | 'video-watch', | |
404 | 'filter:api.video-watch.video.get.params', | |
405 | 'filter:api.video-watch.video.get.result' | |
406 | ) | |
407 | ||
e2f01c47 | 408 | // Video did change |
c8861d5d | 409 | forkJoin([ |
93cae479 | 410 | videoObs, |
e2f01c47 | 411 | this.videoCaptionService.listCaptions(videoId) |
c8861d5d | 412 | ]) |
e2f01c47 | 413 | .pipe( |
ab398a05 | 414 | // If 400, 403 or 404, the video is private or blocked so redirect to 404 |
e6abf95e C |
415 | catchError(err => { |
416 | if (err.body.errorCode === ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS && err.body.originUrl) { | |
417 | const search = window.location.search | |
418 | let originUrl = err.body.originUrl | |
419 | if (search) originUrl += search | |
420 | ||
421 | this.confirmService.confirm( | |
422 | $localize`This video is not available on this instance. Do you want to be redirected on the origin instance: <a href="${originUrl}">${originUrl}</a>?`, | |
423 | $localize`Redirection` | |
424 | ).then(res => { | |
f2eb23cd | 425 | if (res === false) { |
ab398a05 | 426 | return this.restExtractor.redirectTo404IfNotFound(err, 'video', [ |
f2eb23cd | 427 | HttpStatusCode.BAD_REQUEST_400, |
f2eb23cd RK |
428 | HttpStatusCode.FORBIDDEN_403, |
429 | HttpStatusCode.NOT_FOUND_404 | |
430 | ]) | |
431 | } | |
e6abf95e C |
432 | |
433 | return window.location.href = originUrl | |
434 | }) | |
435 | } | |
436 | ||
ab398a05 | 437 | return this.restExtractor.redirectTo404IfNotFound(err, 'video', [ |
f2eb23cd | 438 | HttpStatusCode.BAD_REQUEST_400, |
f2eb23cd RK |
439 | HttpStatusCode.FORBIDDEN_403, |
440 | HttpStatusCode.NOT_FOUND_404 | |
441 | ]) | |
e6abf95e | 442 | }) |
e2f01c47 C |
443 | ) |
444 | .subscribe(([ video, captionsResult ]) => { | |
445 | const queryParams = this.route.snapshot.queryParams | |
e2f01c47 | 446 | |
4c72c1cd | 447 | const urlOptions = { |
3c6a44a1 C |
448 | resume: queryParams.resume, |
449 | ||
4c72c1cd C |
450 | startTime: queryParams.start, |
451 | stopTime: queryParams.stop, | |
5efab546 C |
452 | |
453 | muted: queryParams.muted, | |
454 | loop: queryParams.loop, | |
4c72c1cd | 455 | subtitle: queryParams.subtitle, |
5efab546 C |
456 | |
457 | playerMode: queryParams.mode, | |
458 | peertubeLink: false | |
4c72c1cd C |
459 | } |
460 | ||
461 | this.onVideoFetched(video, captionsResult.data, urlOptions) | |
e2f01c47 C |
462 | .catch(err => this.handleError(err)) |
463 | }) | |
464 | } | |
465 | ||
466 | private loadPlaylist (playlistId: string) { | |
467 | // Playlist did not change | |
468 | if (this.playlist && this.playlist.uuid === playlistId) return | |
469 | ||
470 | this.playlistService.getVideoPlaylist(playlistId) | |
471 | .pipe( | |
ab398a05 RK |
472 | // If 400 or 403, the video is private or blocked so redirect to 404 |
473 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'video', [ | |
f2eb23cd | 474 | HttpStatusCode.BAD_REQUEST_400, |
f2eb23cd RK |
475 | HttpStatusCode.FORBIDDEN_403, |
476 | HttpStatusCode.NOT_FOUND_404 | |
477 | ])) | |
e2f01c47 C |
478 | ) |
479 | .subscribe(playlist => { | |
480 | this.playlist = playlist | |
481 | ||
d142c7b9 | 482 | this.videoWatchPlaylist.loadPlaylistElements(playlist, !this.playlistPosition, this.playlistPosition) |
e2f01c47 C |
483 | }) |
484 | } | |
485 | ||
2de96f4d C |
486 | private updateVideoDescription (description: string) { |
487 | this.video.description = description | |
488 | this.setVideoDescriptionHTML() | |
4c72c1cd | 489 | .catch(err => console.error(err)) |
2de96f4d C |
490 | } |
491 | ||
41d71344 | 492 | private async setVideoDescriptionHTML () { |
d68ebf0b L |
493 | const html = await this.markdownService.textMarkdownToHTML(this.video.description) |
494 | this.videoHTMLDescription = await this.markdownService.processVideoTimestamps(html) | |
2de96f4d C |
495 | } |
496 | ||
e9189001 | 497 | private setVideoLikesBarTooltipText () { |
66357162 | 498 | this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes` |
e9189001 C |
499 | } |
500 | ||
0c31c33d C |
501 | private handleError (err: any) { |
502 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
bf5685f0 C |
503 | if (!errorMessage) return |
504 | ||
6d88de72 | 505 | // Display a message in the video player instead of a notification |
0f7fedc3 | 506 | if (errorMessage.indexOf('from xs param') !== -1) { |
6d88de72 C |
507 | this.flushPlayer() |
508 | this.remoteServerDown = true | |
3b492bff C |
509 | this.changeDetector.detectChanges() |
510 | ||
6d88de72 | 511 | return |
0c31c33d C |
512 | } |
513 | ||
f8b2c1b4 | 514 | this.notifier.error(errorMessage) |
0c31c33d C |
515 | } |
516 | ||
df98563e | 517 | private checkUserRating () { |
d38b8281 | 518 | // Unlogged users do not have ratings |
df98563e | 519 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
520 | |
521 | this.videoService.getUserVideoRating(this.video.id) | |
2186386c C |
522 | .subscribe( |
523 | ratingObject => { | |
524 | if (ratingObject) { | |
525 | this.userRating = ratingObject.rating | |
526 | } | |
527 | }, | |
528 | ||
f8b2c1b4 | 529 | err => this.notifier.error(err.message) |
2186386c | 530 | ) |
d38b8281 C |
531 | } |
532 | ||
597a9266 C |
533 | private async onVideoFetched ( |
534 | video: VideoDetails, | |
535 | videoCaptions: VideoCaption[], | |
a5cf76af | 536 | urlOptions: URLOptions |
597a9266 | 537 | ) { |
a5cf76af C |
538 | this.subscribeToLiveEventsIfNeeded(this.video, video) |
539 | ||
df98563e | 540 | this.video = video |
2f4c784a | 541 | this.videoCaptions = videoCaptions |
92fb909c | 542 | |
c448d412 | 543 | // Re init attributes |
c15d61f5 | 544 | this.playerPlaceholderImgSrc = undefined |
c448d412 C |
545 | this.descriptionLoading = false |
546 | this.completeDescriptionShown = false | |
06bee937 | 547 | this.completeVideoDescription = undefined |
6d88de72 | 548 | this.remoteServerDown = false |
f0a39880 | 549 | this.currentTime = undefined |
c448d412 | 550 | |
e2f01c47 | 551 | if (this.isVideoBlur(this.video)) { |
22b59e80 | 552 | const res = await this.confirmService.confirm( |
66357162 C |
553 | $localize`This video contains mature or explicit content. Are you sure you want to watch it?`, |
554 | $localize`Mature or explicit content` | |
d6e32a2e | 555 | ) |
60c2bc80 | 556 | if (res === false) return this.location.back() |
92fb909c C |
557 | } |
558 | ||
0a6817f0 C |
559 | this.buildPlayer(urlOptions) |
560 | .catch(err => console.error('Cannot build the player', err)) | |
561 | ||
562 | this.setVideoDescriptionHTML() | |
563 | this.setVideoLikesBarTooltipText() | |
564 | ||
565 | this.setOpenGraphTags() | |
566 | this.checkUserRating() | |
567 | ||
55b84d53 C |
568 | const hookOptions = { |
569 | videojs, | |
570 | video: this.video, | |
571 | playlist: this.playlist | |
572 | } | |
573 | this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', hookOptions) | |
0a6817f0 C |
574 | } |
575 | ||
576 | private async buildPlayer (urlOptions: URLOptions) { | |
09edde40 C |
577 | // Flush old player if needed |
578 | this.flushPlayer() | |
b891f9bc | 579 | |
c15d61f5 C |
580 | const videoState = this.video.state.id |
581 | if (videoState === VideoState.LIVE_ENDED || videoState === VideoState.WAITING_FOR_LIVE) { | |
582 | this.playerPlaceholderImgSrc = this.video.previewPath | |
583 | return | |
584 | } | |
585 | ||
60c2bc80 | 586 | // Build video element, because videojs removes it on dispose |
e2f01c47 | 587 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper') |
b891f9bc C |
588 | this.playerElement = document.createElement('video') |
589 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 590 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
591 | playerElementWrapper.appendChild(this.playerElement) |
592 | ||
3d9a63d3 C |
593 | const params = { |
594 | video: this.video, | |
0a6817f0 | 595 | videoCaptions: this.videoCaptions, |
3d9a63d3 C |
596 | urlOptions, |
597 | user: this.user | |
e945b184 | 598 | } |
3d9a63d3 C |
599 | const { playerMode, playerOptions } = await this.hooks.wrapFun( |
600 | this.buildPlayerManagerOptions.bind(this), | |
601 | params, | |
c2023a9f C |
602 | 'video-watch', |
603 | 'filter:internal.video-watch.player.build-options.params', | |
3d9a63d3 C |
604 | 'filter:internal.video-watch.player.build-options.result' |
605 | ) | |
e945b184 | 606 | |
e945b184 | 607 | this.zone.runOutsideAngular(async () => { |
3d9a63d3 | 608 | this.player = await PeertubePlayerManager.initialize(playerMode, playerOptions, player => this.player = player) |
9a18a625 | 609 | |
2adfc7ea | 610 | this.player.on('customError', ({ err }: { err: any }) => this.handleError(err)) |
f0a39880 C |
611 | |
612 | this.player.on('timeupdate', () => { | |
613 | this.currentTime = Math.floor(this.player.currentTime()) | |
614 | }) | |
e2f01c47 | 615 | |
3bcb4fd7 RK |
616 | /** |
617 | * replaces this.player.one('ended') | |
223b24e6 RK |
618 | * 'condition()': true to make the upnext functionality trigger, |
619 | * false to disable the upnext functionality | |
620 | * go to the next video in 'condition()' if you don't want of the timer. | |
621 | * 'next': function triggered at the end of the timer. | |
622 | * 'suspended': function used at each clic of the timer checking if we need | |
623 | * to reset progress and wait until 'suspended' becomes truthy again. | |
3bcb4fd7 RK |
624 | */ |
625 | this.player.upnext({ | |
ddefb8c9 | 626 | timeout: 10000, // 10s |
66357162 C |
627 | headText: $localize`Up Next`, |
628 | cancelText: $localize`Cancel`, | |
629 | suspendedText: $localize`Autoplay is suspended`, | |
3bcb4fd7 RK |
630 | getTitle: () => this.nextVideoTitle, |
631 | next: () => this.zone.run(() => this.autoplayNext()), | |
632 | condition: () => { | |
633 | if (this.playlist) { | |
634 | if (this.isPlaylistAutoPlayEnabled()) { | |
635 | // upnext will not trigger, and instead the next video will play immediately | |
636 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) | |
637 | } | |
638 | } else if (this.isAutoPlayEnabled()) { | |
639 | return true // upnext will trigger | |
640 | } | |
641 | return false // upnext will not trigger, and instead leave the video stopping | |
223b24e6 RK |
642 | }, |
643 | suspended: () => { | |
644 | return ( | |
645 | !isXPercentInViewport(this.player.el(), 80) || | |
646 | !document.getElementById('content').contains(document.activeElement) | |
647 | ) | |
e2f01c47 C |
648 | } |
649 | }) | |
650 | ||
651 | this.player.one('stopped', () => { | |
652 | if (this.playlist) { | |
706c5a47 | 653 | if (this.isPlaylistAutoPlayEnabled()) this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) |
e2f01c47 C |
654 | } |
655 | }) | |
9a18a625 | 656 | |
e772bdf1 C |
657 | this.player.one('ended', () => { |
658 | if (this.video.isLive) { | |
659 | this.video.state.id = VideoState.LIVE_ENDED | |
660 | } | |
661 | }) | |
662 | ||
9a18a625 C |
663 | this.player.on('theaterChange', (_: any, enabled: boolean) => { |
664 | this.zone.run(() => this.theaterEnabled = enabled) | |
665 | }) | |
5f85f8aa | 666 | |
781ba981 | 667 | this.hooks.runAction('action:video-watch.player.loaded', 'video-watch', { player: this.player, videojs, video: this.video }) |
b891f9bc | 668 | }) |
92fb909c C |
669 | } |
670 | ||
6aa54148 | 671 | private autoplayNext () { |
6dd873d6 RK |
672 | if (this.playlist) { |
673 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) | |
674 | } else if (this.nextVideoUuid) { | |
6aa54148 L |
675 | this.router.navigate([ '/videos/watch', this.nextVideoUuid ]) |
676 | } | |
677 | } | |
678 | ||
5c6d985f | 679 | private setRating (nextRating: UserVideoRateType) { |
4c72c1cd C |
680 | const ratingMethods: { [id in UserVideoRateType]: (id: number) => Observable<any> } = { |
681 | like: this.videoService.setVideoLike, | |
682 | dislike: this.videoService.setVideoDislike, | |
683 | none: this.videoService.unsetVideoLike | |
57a49263 BB |
684 | } |
685 | ||
4c72c1cd | 686 | ratingMethods[nextRating].call(this.videoService, this.video.id) |
2186386c C |
687 | .subscribe( |
688 | () => { | |
689 | // Update the video like attribute | |
690 | this.updateVideoRating(this.userRating, nextRating) | |
691 | this.userRating = nextRating | |
692 | }, | |
693 | ||
f8b2c1b4 | 694 | (err: { message: string }) => this.notifier.error(err.message) |
2186386c | 695 | ) |
57a49263 BB |
696 | } |
697 | ||
5c6d985f | 698 | private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) { |
df98563e C |
699 | let likesToIncrement = 0 |
700 | let dislikesToIncrement = 0 | |
d38b8281 C |
701 | |
702 | if (oldRating) { | |
df98563e C |
703 | if (oldRating === 'like') likesToIncrement-- |
704 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
705 | } |
706 | ||
df98563e C |
707 | if (newRating === 'like') likesToIncrement++ |
708 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 709 | |
df98563e C |
710 | this.video.likes += likesToIncrement |
711 | this.video.dislikes += dislikesToIncrement | |
20b40b19 | 712 | |
22b59e80 | 713 | this.video.buildLikeAndDislikePercents() |
20b40b19 | 714 | this.setVideoLikesBarTooltipText() |
d38b8281 C |
715 | } |
716 | ||
df98563e C |
717 | private setOpenGraphTags () { |
718 | this.metaService.setTitle(this.video.name) | |
758b996d | 719 | |
df98563e | 720 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 721 | |
df98563e C |
722 | this.metaService.setTag('og:title', this.video.name) |
723 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 724 | |
df98563e C |
725 | this.metaService.setTag('og:description', this.video.description) |
726 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 727 | |
d38309c3 | 728 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 729 | |
df98563e | 730 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 731 | |
df98563e | 732 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 733 | |
df98563e C |
734 | this.metaService.setTag('og:url', window.location.href) |
735 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 736 | } |
1f3e9fec | 737 | |
d4c6a3b9 | 738 | private isAutoplay () { |
bf079b7b C |
739 | // We'll jump to the thread id, so do not play the video |
740 | if (this.route.snapshot.params['threadId']) return false | |
741 | ||
742 | // Otherwise true by default | |
d4c6a3b9 C |
743 | if (!this.user) return true |
744 | ||
745 | // Be sure the autoPlay is set to false | |
746 | return this.user.autoPlayVideo !== false | |
747 | } | |
09edde40 C |
748 | |
749 | private flushPlayer () { | |
750 | // Remove player if it exists | |
751 | if (this.player) { | |
536598cf C |
752 | try { |
753 | this.player.dispose() | |
754 | this.player = undefined | |
755 | } catch (err) { | |
756 | console.error('Cannot dispose player.', err) | |
757 | } | |
09edde40 C |
758 | } |
759 | } | |
1c8ddbfa | 760 | |
3d9a63d3 C |
761 | private buildPlayerManagerOptions (params: { |
762 | video: VideoDetails, | |
763 | videoCaptions: VideoCaption[], | |
764 | urlOptions: CustomizationOptions & { playerMode: PlayerMode }, | |
765 | user?: AuthUser | |
766 | }) { | |
767 | const { video, videoCaptions, urlOptions, user } = params | |
706c5a47 RK |
768 | const getStartTime = () => { |
769 | const byUrl = urlOptions.startTime !== undefined | |
96f6278f | 770 | const byHistory = video.userHistory && (!this.playlist || urlOptions.resume !== undefined) |
58b9ce30 | 771 | const byLocalStorage = getStoredVideoWatchHistory(video.uuid) |
706c5a47 | 772 | |
3c6a44a1 C |
773 | if (byUrl) return timeToInt(urlOptions.startTime) |
774 | if (byHistory) return video.userHistory.currentTime | |
58b9ce30 | 775 | if (byLocalStorage) return byLocalStorage.duration |
3c6a44a1 C |
776 | |
777 | return 0 | |
706c5a47 | 778 | } |
3d9a63d3 | 779 | |
706c5a47 | 780 | let startTime = getStartTime() |
3c6a44a1 | 781 | |
3d9a63d3 C |
782 | // If we are at the end of the video, reset the timer |
783 | if (video.duration - startTime <= 1) startTime = 0 | |
784 | ||
785 | const playerCaptions = videoCaptions.map(c => ({ | |
786 | label: c.language.label, | |
787 | language: c.language.id, | |
788 | src: environment.apiUrl + c.captionPath | |
789 | })) | |
790 | ||
791 | const options: PeertubePlayerManagerOptions = { | |
792 | common: { | |
793 | autoplay: this.isAutoplay(), | |
1dc240a9 | 794 | nextVideo: () => this.zone.run(() => this.autoplayNext()), |
3d9a63d3 C |
795 | |
796 | playerElement: this.playerElement, | |
797 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, | |
798 | ||
799 | videoDuration: video.duration, | |
800 | enableHotkeys: true, | |
801 | inactivityTimeout: 2500, | |
802 | poster: video.previewUrl, | |
803 | ||
804 | startTime, | |
805 | stopTime: urlOptions.stopTime, | |
806 | controls: urlOptions.controls, | |
807 | muted: urlOptions.muted, | |
808 | loop: urlOptions.loop, | |
809 | subtitle: urlOptions.subtitle, | |
810 | ||
811 | peertubeLink: urlOptions.peertubeLink, | |
812 | ||
813 | theaterButton: true, | |
814 | captions: videoCaptions.length !== 0, | |
815 | ||
816 | videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE | |
817 | ? this.videoService.getVideoViewUrl(video.uuid) | |
818 | : null, | |
819 | embedUrl: video.embedUrl, | |
4097c6d6 | 820 | embedTitle: video.name, |
3d9a63d3 | 821 | |
25b7c847 C |
822 | isLive: video.isLive, |
823 | ||
3d9a63d3 C |
824 | language: this.localeId, |
825 | ||
826 | userWatching: user && user.videosHistoryEnabled === true ? { | |
827 | url: this.videoService.getUserWatchingVideoUrl(video.uuid), | |
828 | authorizationHeader: this.authService.getRequestHeaderValue() | |
829 | } : undefined, | |
830 | ||
831 | serverUrl: environment.apiUrl, | |
832 | ||
58b9ce30 | 833 | videoCaptions: playerCaptions, |
834 | ||
835 | videoUUID: video.uuid | |
3d9a63d3 C |
836 | }, |
837 | ||
838 | webtorrent: { | |
839 | videoFiles: video.files | |
840 | } | |
841 | } | |
842 | ||
843 | let mode: PlayerMode | |
844 | ||
845 | if (urlOptions.playerMode) { | |
846 | if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader' | |
847 | else mode = 'webtorrent' | |
848 | } else { | |
849 | if (video.hasHlsPlaylist()) mode = 'p2p-media-loader' | |
850 | else mode = 'webtorrent' | |
851 | } | |
852 | ||
089af69b C |
853 | // p2p-media-loader needs TextEncoder, try to fallback on WebTorrent |
854 | if (typeof TextEncoder === 'undefined') { | |
855 | mode = 'webtorrent' | |
856 | } | |
857 | ||
3d9a63d3 C |
858 | if (mode === 'p2p-media-loader') { |
859 | const hlsPlaylist = video.getHlsPlaylist() | |
860 | ||
861 | const p2pMediaLoader = { | |
862 | playlistUrl: hlsPlaylist.playlistUrl, | |
863 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
864 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
865 | trackerAnnounce: video.trackerUrls, | |
866 | videoFiles: hlsPlaylist.files | |
867 | } as P2PMediaLoaderOptions | |
868 | ||
869 | Object.assign(options, { p2pMediaLoader }) | |
870 | } | |
871 | ||
872 | return { playerMode: mode, playerOptions: options } | |
873 | } | |
874 | ||
a5cf76af C |
875 | private async subscribeToLiveEventsIfNeeded (oldVideo: VideoDetails, newVideo: VideoDetails) { |
876 | if (!this.liveVideosSub) { | |
a800dbf3 | 877 | this.liveVideosSub = this.buildLiveEventsSubscription() |
a5cf76af C |
878 | } |
879 | ||
880 | if (oldVideo && oldVideo.id !== newVideo.id) { | |
881 | await this.peertubeSocket.unsubscribeLiveVideos(oldVideo.id) | |
882 | } | |
883 | ||
884 | if (!newVideo.isLive) return | |
885 | ||
886 | await this.peertubeSocket.subscribeToLiveVideosSocket(newVideo.id) | |
887 | } | |
888 | ||
a800dbf3 C |
889 | private buildLiveEventsSubscription () { |
890 | return this.peertubeSocket.getLiveVideosObservable() | |
891 | .subscribe(({ type, payload }) => { | |
892 | if (type === 'state-change') return this.handleLiveStateChange(payload.state) | |
893 | if (type === 'views-change') return this.handleLiveViewsChange(payload.views) | |
894 | }) | |
895 | } | |
896 | ||
897 | private handleLiveStateChange (newState: VideoState) { | |
898 | if (newState !== VideoState.PUBLISHED) return | |
899 | ||
900 | const videoState = this.video.state.id | |
901 | if (videoState !== VideoState.WAITING_FOR_LIVE && videoState !== VideoState.LIVE_ENDED) return | |
902 | ||
903 | console.log('Loading video after live update.') | |
904 | ||
905 | const videoUUID = this.video.uuid | |
906 | ||
907 | // Reset to refetch the video | |
908 | this.video = undefined | |
909 | this.loadVideo(videoUUID) | |
910 | } | |
911 | ||
912 | private handleLiveViewsChange (newViews: number) { | |
913 | if (!this.video) { | |
914 | console.error('Cannot update video live views because video is no defined.') | |
915 | return | |
916 | } | |
917 | ||
e43b5a3f C |
918 | console.log('Updating live views.') |
919 | ||
a800dbf3 C |
920 | this.video.views = newViews |
921 | } | |
922 | ||
941c5eac C |
923 | private initHotkeys () { |
924 | this.hotkeys = [ | |
941c5eac | 925 | // These hotkeys are managed by the player |
66357162 C |
926 | new Hotkey('f', e => e, undefined, $localize`Enter/exit fullscreen (requires player focus)`), |
927 | new Hotkey('space', e => e, undefined, $localize`Play/Pause the video (requires player focus)`), | |
928 | new Hotkey('m', e => e, undefined, $localize`Mute/unmute the video (requires player focus)`), | |
941c5eac | 929 | |
66357162 | 930 | new Hotkey('0-9', e => e, undefined, $localize`Skip to a percentage of the video: 0 is 0% and 9 is 90% (requires player focus)`), |
941c5eac | 931 | |
66357162 C |
932 | new Hotkey('up', e => e, undefined, $localize`Increase the volume (requires player focus)`), |
933 | new Hotkey('down', e => e, undefined, $localize`Decrease the volume (requires player focus)`), | |
941c5eac | 934 | |
66357162 C |
935 | new Hotkey('right', e => e, undefined, $localize`Seek the video forward (requires player focus)`), |
936 | new Hotkey('left', e => e, undefined, $localize`Seek the video backward (requires player focus)`), | |
941c5eac | 937 | |
66357162 C |
938 | new Hotkey('>', e => e, undefined, $localize`Increase playback rate (requires player focus)`), |
939 | new Hotkey('<', e => e, undefined, $localize`Decrease playback rate (requires player focus)`), | |
941c5eac | 940 | |
66357162 | 941 | new Hotkey('.', e => e, undefined, $localize`Navigate in the video frame by frame (requires player focus)`) |
941c5eac | 942 | ] |
3d216ea0 C |
943 | |
944 | if (this.isUserLoggedIn()) { | |
945 | this.hotkeys = this.hotkeys.concat([ | |
946 | new Hotkey('shift+l', () => { | |
947 | this.setLike() | |
948 | return false | |
66357162 | 949 | }, undefined, $localize`Like the video`), |
3d216ea0 C |
950 | |
951 | new Hotkey('shift+d', () => { | |
952 | this.setDislike() | |
953 | return false | |
66357162 | 954 | }, undefined, $localize`Dislike the video`), |
3d216ea0 C |
955 | |
956 | new Hotkey('shift+s', () => { | |
957 | this.subscribeButton.subscribed ? this.subscribeButton.unsubscribe() : this.subscribeButton.subscribe() | |
958 | return false | |
66357162 | 959 | }, undefined, $localize`Subscribe to the account`) |
3d216ea0 C |
960 | ]) |
961 | } | |
962 | ||
963 | this.hotkeysService.add(this.hotkeys) | |
941c5eac | 964 | } |
dc8bc31b | 965 | } |