]>
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 | ||
de779034 RK |
298 | getVideoUrl () { |
299 | if (!this.video.url) { | |
300 | return this.video.originInstanceUrl + VideoDetails.buildClientUrl(this.video.uuid) | |
301 | } | |
302 | return this.video.url | |
303 | } | |
304 | ||
b1fa3eba C |
305 | getVideoTags () { |
306 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
307 | ||
4278710d | 308 | return this.video.tags |
b1fa3eba C |
309 | } |
310 | ||
6aa54148 L |
311 | onRecommendations (videos: Video[]) { |
312 | if (videos.length > 0) { | |
3bcb4fd7 RK |
313 | // The recommended videos's first element should be the next video |
314 | const video = videos[0] | |
315 | this.nextVideoUuid = video.uuid | |
316 | this.nextVideoTitle = video.name | |
6aa54148 L |
317 | } |
318 | } | |
319 | ||
3a0fb65c C |
320 | onVideoRemoved () { |
321 | this.redirectService.redirectToHomepage() | |
6725d05c C |
322 | } |
323 | ||
d3217560 RK |
324 | declinedPrivacyConcern () { |
325 | peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'false') | |
326 | this.hasAlreadyAcceptedPrivacyConcern = false | |
327 | } | |
328 | ||
73e09f27 | 329 | acceptedPrivacyConcern () { |
0bd78bf3 | 330 | peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true') |
73e09f27 C |
331 | this.hasAlreadyAcceptedPrivacyConcern = true |
332 | } | |
333 | ||
2186386c C |
334 | isVideoToTranscode () { |
335 | return this.video && this.video.state.id === VideoState.TO_TRANSCODE | |
336 | } | |
337 | ||
516df59b C |
338 | isVideoToImport () { |
339 | return this.video && this.video.state.id === VideoState.TO_IMPORT | |
340 | } | |
341 | ||
bbe0f064 C |
342 | hasVideoScheduledPublication () { |
343 | return this.video && this.video.scheduledUpdate !== undefined | |
344 | } | |
345 | ||
a5cf76af C |
346 | isLive () { |
347 | return !!(this.video?.isLive) | |
348 | } | |
349 | ||
350 | isWaitingForLive () { | |
351 | return this.video?.state.id === VideoState.WAITING_FOR_LIVE | |
352 | } | |
353 | ||
354 | isLiveEnded () { | |
355 | return this.video?.state.id === VideoState.LIVE_ENDED | |
356 | } | |
357 | ||
e2f01c47 | 358 | isVideoBlur (video: Video) { |
ba430d75 | 359 | return video.isVideoNSFWForUser(this.user, this.serverConfig) |
e2f01c47 C |
360 | } |
361 | ||
706c5a47 RK |
362 | isAutoPlayEnabled () { |
363 | return ( | |
7c93905d | 364 | (this.user && this.user.autoPlayNextVideo) || |
d3217560 | 365 | this.anonymousUser.autoPlayNextVideo |
706c5a47 | 366 | ) |
b29bf61d RK |
367 | } |
368 | ||
369 | handleTimestampClicked (timestamp: number) { | |
18429d01 C |
370 | if (!this.player || this.video.isLive) return |
371 | ||
372 | this.player.currentTime(timestamp) | |
b29bf61d | 373 | scrollToTop() |
706c5a47 RK |
374 | } |
375 | ||
376 | isPlaylistAutoPlayEnabled () { | |
377 | return ( | |
7c93905d | 378 | (this.user && this.user.autoPlayNextVideoPlaylist) || |
d3217560 | 379 | this.anonymousUser.autoPlayNextVideoPlaylist |
706c5a47 RK |
380 | ) |
381 | } | |
382 | ||
b40a2193 K |
383 | isChannelDisplayNameGeneric () { |
384 | const genericChannelDisplayName = [ | |
385 | `Main ${this.video.channel.ownerAccount.name} channel`, | |
386 | `Default ${this.video.channel.ownerAccount.name} channel` | |
387 | ] | |
388 | ||
389 | return genericChannelDisplayName.includes(this.video.channel.displayName) | |
390 | } | |
391 | ||
d142c7b9 C |
392 | onPlaylistVideoFound (videoId: string) { |
393 | this.loadVideo(videoId) | |
394 | } | |
395 | ||
0f7407d9 C |
396 | displayOtherVideosAsRow () { |
397 | // Use the same value as in the SASS file | |
398 | return this.screenService.getWindowInnerWidth() <= 1100 | |
399 | } | |
400 | ||
e2f01c47 C |
401 | private loadVideo (videoId: string) { |
402 | // Video did not change | |
403 | if (this.video && this.video.uuid === videoId) return | |
404 | ||
405 | if (this.player) this.player.pause() | |
406 | ||
93cae479 C |
407 | const videoObs = this.hooks.wrapObsFun( |
408 | this.videoService.getVideo.bind(this.videoService), | |
409 | { videoId }, | |
410 | 'video-watch', | |
411 | 'filter:api.video-watch.video.get.params', | |
412 | 'filter:api.video-watch.video.get.result' | |
413 | ) | |
414 | ||
e2f01c47 | 415 | // Video did change |
c8861d5d | 416 | forkJoin([ |
93cae479 | 417 | videoObs, |
e2f01c47 | 418 | this.videoCaptionService.listCaptions(videoId) |
c8861d5d | 419 | ]) |
e2f01c47 | 420 | .pipe( |
ab398a05 | 421 | // If 400, 403 or 404, the video is private or blocked so redirect to 404 |
e6abf95e C |
422 | catchError(err => { |
423 | if (err.body.errorCode === ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS && err.body.originUrl) { | |
424 | const search = window.location.search | |
425 | let originUrl = err.body.originUrl | |
426 | if (search) originUrl += search | |
427 | ||
428 | this.confirmService.confirm( | |
429 | $localize`This video is not available on this instance. Do you want to be redirected on the origin instance: <a href="${originUrl}">${originUrl}</a>?`, | |
430 | $localize`Redirection` | |
431 | ).then(res => { | |
f2eb23cd | 432 | if (res === false) { |
ab398a05 | 433 | return this.restExtractor.redirectTo404IfNotFound(err, 'video', [ |
f2eb23cd | 434 | HttpStatusCode.BAD_REQUEST_400, |
f2eb23cd RK |
435 | HttpStatusCode.FORBIDDEN_403, |
436 | HttpStatusCode.NOT_FOUND_404 | |
437 | ]) | |
438 | } | |
e6abf95e C |
439 | |
440 | return window.location.href = originUrl | |
441 | }) | |
442 | } | |
443 | ||
ab398a05 | 444 | return this.restExtractor.redirectTo404IfNotFound(err, 'video', [ |
f2eb23cd | 445 | HttpStatusCode.BAD_REQUEST_400, |
f2eb23cd RK |
446 | HttpStatusCode.FORBIDDEN_403, |
447 | HttpStatusCode.NOT_FOUND_404 | |
448 | ]) | |
e6abf95e | 449 | }) |
e2f01c47 C |
450 | ) |
451 | .subscribe(([ video, captionsResult ]) => { | |
452 | const queryParams = this.route.snapshot.queryParams | |
e2f01c47 | 453 | |
4c72c1cd | 454 | const urlOptions = { |
3c6a44a1 C |
455 | resume: queryParams.resume, |
456 | ||
4c72c1cd C |
457 | startTime: queryParams.start, |
458 | stopTime: queryParams.stop, | |
5efab546 C |
459 | |
460 | muted: queryParams.muted, | |
461 | loop: queryParams.loop, | |
4c72c1cd | 462 | subtitle: queryParams.subtitle, |
5efab546 C |
463 | |
464 | playerMode: queryParams.mode, | |
465 | peertubeLink: false | |
4c72c1cd C |
466 | } |
467 | ||
468 | this.onVideoFetched(video, captionsResult.data, urlOptions) | |
e2f01c47 C |
469 | .catch(err => this.handleError(err)) |
470 | }) | |
471 | } | |
472 | ||
473 | private loadPlaylist (playlistId: string) { | |
474 | // Playlist did not change | |
475 | if (this.playlist && this.playlist.uuid === playlistId) return | |
476 | ||
477 | this.playlistService.getVideoPlaylist(playlistId) | |
478 | .pipe( | |
ab398a05 RK |
479 | // If 400 or 403, the video is private or blocked so redirect to 404 |
480 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'video', [ | |
f2eb23cd | 481 | HttpStatusCode.BAD_REQUEST_400, |
f2eb23cd RK |
482 | HttpStatusCode.FORBIDDEN_403, |
483 | HttpStatusCode.NOT_FOUND_404 | |
484 | ])) | |
e2f01c47 C |
485 | ) |
486 | .subscribe(playlist => { | |
487 | this.playlist = playlist | |
488 | ||
d142c7b9 | 489 | this.videoWatchPlaylist.loadPlaylistElements(playlist, !this.playlistPosition, this.playlistPosition) |
e2f01c47 C |
490 | }) |
491 | } | |
492 | ||
2de96f4d C |
493 | private updateVideoDescription (description: string) { |
494 | this.video.description = description | |
495 | this.setVideoDescriptionHTML() | |
4c72c1cd | 496 | .catch(err => console.error(err)) |
2de96f4d C |
497 | } |
498 | ||
41d71344 | 499 | private async setVideoDescriptionHTML () { |
d68ebf0b L |
500 | const html = await this.markdownService.textMarkdownToHTML(this.video.description) |
501 | this.videoHTMLDescription = await this.markdownService.processVideoTimestamps(html) | |
2de96f4d C |
502 | } |
503 | ||
e9189001 | 504 | private setVideoLikesBarTooltipText () { |
66357162 | 505 | this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes` |
e9189001 C |
506 | } |
507 | ||
0c31c33d C |
508 | private handleError (err: any) { |
509 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
bf5685f0 C |
510 | if (!errorMessage) return |
511 | ||
6d88de72 | 512 | // Display a message in the video player instead of a notification |
0f7fedc3 | 513 | if (errorMessage.indexOf('from xs param') !== -1) { |
6d88de72 C |
514 | this.flushPlayer() |
515 | this.remoteServerDown = true | |
3b492bff C |
516 | this.changeDetector.detectChanges() |
517 | ||
6d88de72 | 518 | return |
0c31c33d C |
519 | } |
520 | ||
f8b2c1b4 | 521 | this.notifier.error(errorMessage) |
0c31c33d C |
522 | } |
523 | ||
df98563e | 524 | private checkUserRating () { |
d38b8281 | 525 | // Unlogged users do not have ratings |
df98563e | 526 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
527 | |
528 | this.videoService.getUserVideoRating(this.video.id) | |
2186386c C |
529 | .subscribe( |
530 | ratingObject => { | |
531 | if (ratingObject) { | |
532 | this.userRating = ratingObject.rating | |
533 | } | |
534 | }, | |
535 | ||
f8b2c1b4 | 536 | err => this.notifier.error(err.message) |
2186386c | 537 | ) |
d38b8281 C |
538 | } |
539 | ||
597a9266 C |
540 | private async onVideoFetched ( |
541 | video: VideoDetails, | |
542 | videoCaptions: VideoCaption[], | |
a5cf76af | 543 | urlOptions: URLOptions |
597a9266 | 544 | ) { |
a5cf76af C |
545 | this.subscribeToLiveEventsIfNeeded(this.video, video) |
546 | ||
df98563e | 547 | this.video = video |
2f4c784a | 548 | this.videoCaptions = videoCaptions |
92fb909c | 549 | |
c448d412 | 550 | // Re init attributes |
c15d61f5 | 551 | this.playerPlaceholderImgSrc = undefined |
c448d412 C |
552 | this.descriptionLoading = false |
553 | this.completeDescriptionShown = false | |
06bee937 | 554 | this.completeVideoDescription = undefined |
6d88de72 | 555 | this.remoteServerDown = false |
f0a39880 | 556 | this.currentTime = undefined |
c448d412 | 557 | |
e2f01c47 | 558 | if (this.isVideoBlur(this.video)) { |
22b59e80 | 559 | const res = await this.confirmService.confirm( |
66357162 C |
560 | $localize`This video contains mature or explicit content. Are you sure you want to watch it?`, |
561 | $localize`Mature or explicit content` | |
d6e32a2e | 562 | ) |
60c2bc80 | 563 | if (res === false) return this.location.back() |
92fb909c C |
564 | } |
565 | ||
0a6817f0 C |
566 | this.buildPlayer(urlOptions) |
567 | .catch(err => console.error('Cannot build the player', err)) | |
568 | ||
569 | this.setVideoDescriptionHTML() | |
570 | this.setVideoLikesBarTooltipText() | |
571 | ||
572 | this.setOpenGraphTags() | |
573 | this.checkUserRating() | |
574 | ||
55b84d53 C |
575 | const hookOptions = { |
576 | videojs, | |
577 | video: this.video, | |
578 | playlist: this.playlist | |
579 | } | |
580 | this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', hookOptions) | |
0a6817f0 C |
581 | } |
582 | ||
583 | private async buildPlayer (urlOptions: URLOptions) { | |
09edde40 C |
584 | // Flush old player if needed |
585 | this.flushPlayer() | |
b891f9bc | 586 | |
c15d61f5 C |
587 | const videoState = this.video.state.id |
588 | if (videoState === VideoState.LIVE_ENDED || videoState === VideoState.WAITING_FOR_LIVE) { | |
589 | this.playerPlaceholderImgSrc = this.video.previewPath | |
590 | return | |
591 | } | |
592 | ||
60c2bc80 | 593 | // Build video element, because videojs removes it on dispose |
e2f01c47 | 594 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper') |
b891f9bc C |
595 | this.playerElement = document.createElement('video') |
596 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 597 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
598 | playerElementWrapper.appendChild(this.playerElement) |
599 | ||
3d9a63d3 C |
600 | const params = { |
601 | video: this.video, | |
0a6817f0 | 602 | videoCaptions: this.videoCaptions, |
3d9a63d3 C |
603 | urlOptions, |
604 | user: this.user | |
e945b184 | 605 | } |
3d9a63d3 C |
606 | const { playerMode, playerOptions } = await this.hooks.wrapFun( |
607 | this.buildPlayerManagerOptions.bind(this), | |
608 | params, | |
c2023a9f C |
609 | 'video-watch', |
610 | 'filter:internal.video-watch.player.build-options.params', | |
3d9a63d3 C |
611 | 'filter:internal.video-watch.player.build-options.result' |
612 | ) | |
e945b184 | 613 | |
e945b184 | 614 | this.zone.runOutsideAngular(async () => { |
3d9a63d3 | 615 | this.player = await PeertubePlayerManager.initialize(playerMode, playerOptions, player => this.player = player) |
9a18a625 | 616 | |
2adfc7ea | 617 | this.player.on('customError', ({ err }: { err: any }) => this.handleError(err)) |
f0a39880 C |
618 | |
619 | this.player.on('timeupdate', () => { | |
620 | this.currentTime = Math.floor(this.player.currentTime()) | |
621 | }) | |
e2f01c47 | 622 | |
3bcb4fd7 RK |
623 | /** |
624 | * replaces this.player.one('ended') | |
223b24e6 RK |
625 | * 'condition()': true to make the upnext functionality trigger, |
626 | * false to disable the upnext functionality | |
627 | * go to the next video in 'condition()' if you don't want of the timer. | |
628 | * 'next': function triggered at the end of the timer. | |
629 | * 'suspended': function used at each clic of the timer checking if we need | |
630 | * to reset progress and wait until 'suspended' becomes truthy again. | |
3bcb4fd7 RK |
631 | */ |
632 | this.player.upnext({ | |
ddefb8c9 | 633 | timeout: 10000, // 10s |
66357162 C |
634 | headText: $localize`Up Next`, |
635 | cancelText: $localize`Cancel`, | |
636 | suspendedText: $localize`Autoplay is suspended`, | |
3bcb4fd7 RK |
637 | getTitle: () => this.nextVideoTitle, |
638 | next: () => this.zone.run(() => this.autoplayNext()), | |
639 | condition: () => { | |
640 | if (this.playlist) { | |
641 | if (this.isPlaylistAutoPlayEnabled()) { | |
642 | // upnext will not trigger, and instead the next video will play immediately | |
643 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) | |
644 | } | |
645 | } else if (this.isAutoPlayEnabled()) { | |
646 | return true // upnext will trigger | |
647 | } | |
648 | return false // upnext will not trigger, and instead leave the video stopping | |
223b24e6 RK |
649 | }, |
650 | suspended: () => { | |
651 | return ( | |
652 | !isXPercentInViewport(this.player.el(), 80) || | |
653 | !document.getElementById('content').contains(document.activeElement) | |
654 | ) | |
e2f01c47 C |
655 | } |
656 | }) | |
657 | ||
658 | this.player.one('stopped', () => { | |
659 | if (this.playlist) { | |
706c5a47 | 660 | if (this.isPlaylistAutoPlayEnabled()) this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) |
e2f01c47 C |
661 | } |
662 | }) | |
9a18a625 | 663 | |
e772bdf1 C |
664 | this.player.one('ended', () => { |
665 | if (this.video.isLive) { | |
666 | this.video.state.id = VideoState.LIVE_ENDED | |
667 | } | |
668 | }) | |
669 | ||
9a18a625 C |
670 | this.player.on('theaterChange', (_: any, enabled: boolean) => { |
671 | this.zone.run(() => this.theaterEnabled = enabled) | |
672 | }) | |
5f85f8aa | 673 | |
781ba981 | 674 | this.hooks.runAction('action:video-watch.player.loaded', 'video-watch', { player: this.player, videojs, video: this.video }) |
b891f9bc | 675 | }) |
92fb909c C |
676 | } |
677 | ||
6aa54148 | 678 | private autoplayNext () { |
6dd873d6 RK |
679 | if (this.playlist) { |
680 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) | |
681 | } else if (this.nextVideoUuid) { | |
6aa54148 L |
682 | this.router.navigate([ '/videos/watch', this.nextVideoUuid ]) |
683 | } | |
684 | } | |
685 | ||
5c6d985f | 686 | private setRating (nextRating: UserVideoRateType) { |
4c72c1cd C |
687 | const ratingMethods: { [id in UserVideoRateType]: (id: number) => Observable<any> } = { |
688 | like: this.videoService.setVideoLike, | |
689 | dislike: this.videoService.setVideoDislike, | |
690 | none: this.videoService.unsetVideoLike | |
57a49263 BB |
691 | } |
692 | ||
4c72c1cd | 693 | ratingMethods[nextRating].call(this.videoService, this.video.id) |
2186386c C |
694 | .subscribe( |
695 | () => { | |
696 | // Update the video like attribute | |
697 | this.updateVideoRating(this.userRating, nextRating) | |
698 | this.userRating = nextRating | |
699 | }, | |
700 | ||
f8b2c1b4 | 701 | (err: { message: string }) => this.notifier.error(err.message) |
2186386c | 702 | ) |
57a49263 BB |
703 | } |
704 | ||
5c6d985f | 705 | private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) { |
df98563e C |
706 | let likesToIncrement = 0 |
707 | let dislikesToIncrement = 0 | |
d38b8281 C |
708 | |
709 | if (oldRating) { | |
df98563e C |
710 | if (oldRating === 'like') likesToIncrement-- |
711 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
712 | } |
713 | ||
df98563e C |
714 | if (newRating === 'like') likesToIncrement++ |
715 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 716 | |
df98563e C |
717 | this.video.likes += likesToIncrement |
718 | this.video.dislikes += dislikesToIncrement | |
20b40b19 | 719 | |
22b59e80 | 720 | this.video.buildLikeAndDislikePercents() |
20b40b19 | 721 | this.setVideoLikesBarTooltipText() |
d38b8281 C |
722 | } |
723 | ||
df98563e C |
724 | private setOpenGraphTags () { |
725 | this.metaService.setTitle(this.video.name) | |
758b996d | 726 | |
df98563e | 727 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 728 | |
df98563e C |
729 | this.metaService.setTag('og:title', this.video.name) |
730 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 731 | |
df98563e C |
732 | this.metaService.setTag('og:description', this.video.description) |
733 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 734 | |
d38309c3 | 735 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 736 | |
df98563e | 737 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 738 | |
df98563e | 739 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 740 | |
df98563e C |
741 | this.metaService.setTag('og:url', window.location.href) |
742 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 743 | } |
1f3e9fec | 744 | |
d4c6a3b9 | 745 | private isAutoplay () { |
bf079b7b C |
746 | // We'll jump to the thread id, so do not play the video |
747 | if (this.route.snapshot.params['threadId']) return false | |
748 | ||
749 | // Otherwise true by default | |
d4c6a3b9 C |
750 | if (!this.user) return true |
751 | ||
752 | // Be sure the autoPlay is set to false | |
753 | return this.user.autoPlayVideo !== false | |
754 | } | |
09edde40 C |
755 | |
756 | private flushPlayer () { | |
757 | // Remove player if it exists | |
758 | if (this.player) { | |
536598cf C |
759 | try { |
760 | this.player.dispose() | |
761 | this.player = undefined | |
762 | } catch (err) { | |
763 | console.error('Cannot dispose player.', err) | |
764 | } | |
09edde40 C |
765 | } |
766 | } | |
1c8ddbfa | 767 | |
3d9a63d3 C |
768 | private buildPlayerManagerOptions (params: { |
769 | video: VideoDetails, | |
770 | videoCaptions: VideoCaption[], | |
771 | urlOptions: CustomizationOptions & { playerMode: PlayerMode }, | |
772 | user?: AuthUser | |
773 | }) { | |
774 | const { video, videoCaptions, urlOptions, user } = params | |
706c5a47 RK |
775 | const getStartTime = () => { |
776 | const byUrl = urlOptions.startTime !== undefined | |
96f6278f | 777 | const byHistory = video.userHistory && (!this.playlist || urlOptions.resume !== undefined) |
58b9ce30 | 778 | const byLocalStorage = getStoredVideoWatchHistory(video.uuid) |
706c5a47 | 779 | |
3c6a44a1 C |
780 | if (byUrl) return timeToInt(urlOptions.startTime) |
781 | if (byHistory) return video.userHistory.currentTime | |
58b9ce30 | 782 | if (byLocalStorage) return byLocalStorage.duration |
3c6a44a1 C |
783 | |
784 | return 0 | |
706c5a47 | 785 | } |
3d9a63d3 | 786 | |
706c5a47 | 787 | let startTime = getStartTime() |
3c6a44a1 | 788 | |
3d9a63d3 C |
789 | // If we are at the end of the video, reset the timer |
790 | if (video.duration - startTime <= 1) startTime = 0 | |
791 | ||
792 | const playerCaptions = videoCaptions.map(c => ({ | |
793 | label: c.language.label, | |
794 | language: c.language.id, | |
795 | src: environment.apiUrl + c.captionPath | |
796 | })) | |
797 | ||
798 | const options: PeertubePlayerManagerOptions = { | |
799 | common: { | |
800 | autoplay: this.isAutoplay(), | |
1dc240a9 | 801 | nextVideo: () => this.zone.run(() => this.autoplayNext()), |
33d21a9b P |
802 | previousVideo: () => this.zone.run(() => { |
803 | // FIXME: Only show if this is a playlist | |
804 | if (this.playlist) this.zone.run(() => this.videoWatchPlaylist.navigateToPreviousPlaylistVideo()) | |
805 | }), | |
3d9a63d3 C |
806 | |
807 | playerElement: this.playerElement, | |
808 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, | |
809 | ||
810 | videoDuration: video.duration, | |
811 | enableHotkeys: true, | |
812 | inactivityTimeout: 2500, | |
813 | poster: video.previewUrl, | |
814 | ||
815 | startTime, | |
816 | stopTime: urlOptions.stopTime, | |
817 | controls: urlOptions.controls, | |
818 | muted: urlOptions.muted, | |
819 | loop: urlOptions.loop, | |
820 | subtitle: urlOptions.subtitle, | |
821 | ||
822 | peertubeLink: urlOptions.peertubeLink, | |
823 | ||
824 | theaterButton: true, | |
825 | captions: videoCaptions.length !== 0, | |
826 | ||
827 | videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE | |
828 | ? this.videoService.getVideoViewUrl(video.uuid) | |
829 | : null, | |
830 | embedUrl: video.embedUrl, | |
4097c6d6 | 831 | embedTitle: video.name, |
3d9a63d3 | 832 | |
25b7c847 C |
833 | isLive: video.isLive, |
834 | ||
3d9a63d3 C |
835 | language: this.localeId, |
836 | ||
837 | userWatching: user && user.videosHistoryEnabled === true ? { | |
838 | url: this.videoService.getUserWatchingVideoUrl(video.uuid), | |
839 | authorizationHeader: this.authService.getRequestHeaderValue() | |
840 | } : undefined, | |
841 | ||
842 | serverUrl: environment.apiUrl, | |
843 | ||
58b9ce30 | 844 | videoCaptions: playerCaptions, |
845 | ||
3e0e8d4a | 846 | videoUUID: video.uuid |
3d9a63d3 C |
847 | }, |
848 | ||
849 | webtorrent: { | |
850 | videoFiles: video.files | |
851 | } | |
852 | } | |
853 | ||
854 | let mode: PlayerMode | |
855 | ||
856 | if (urlOptions.playerMode) { | |
857 | if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader' | |
858 | else mode = 'webtorrent' | |
859 | } else { | |
860 | if (video.hasHlsPlaylist()) mode = 'p2p-media-loader' | |
861 | else mode = 'webtorrent' | |
862 | } | |
863 | ||
089af69b C |
864 | // p2p-media-loader needs TextEncoder, try to fallback on WebTorrent |
865 | if (typeof TextEncoder === 'undefined') { | |
866 | mode = 'webtorrent' | |
867 | } | |
868 | ||
3d9a63d3 C |
869 | if (mode === 'p2p-media-loader') { |
870 | const hlsPlaylist = video.getHlsPlaylist() | |
871 | ||
872 | const p2pMediaLoader = { | |
873 | playlistUrl: hlsPlaylist.playlistUrl, | |
874 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
875 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
876 | trackerAnnounce: video.trackerUrls, | |
877 | videoFiles: hlsPlaylist.files | |
878 | } as P2PMediaLoaderOptions | |
879 | ||
880 | Object.assign(options, { p2pMediaLoader }) | |
881 | } | |
882 | ||
883 | return { playerMode: mode, playerOptions: options } | |
884 | } | |
885 | ||
a5cf76af C |
886 | private async subscribeToLiveEventsIfNeeded (oldVideo: VideoDetails, newVideo: VideoDetails) { |
887 | if (!this.liveVideosSub) { | |
a800dbf3 | 888 | this.liveVideosSub = this.buildLiveEventsSubscription() |
a5cf76af C |
889 | } |
890 | ||
891 | if (oldVideo && oldVideo.id !== newVideo.id) { | |
892 | await this.peertubeSocket.unsubscribeLiveVideos(oldVideo.id) | |
893 | } | |
894 | ||
895 | if (!newVideo.isLive) return | |
896 | ||
897 | await this.peertubeSocket.subscribeToLiveVideosSocket(newVideo.id) | |
898 | } | |
899 | ||
a800dbf3 C |
900 | private buildLiveEventsSubscription () { |
901 | return this.peertubeSocket.getLiveVideosObservable() | |
902 | .subscribe(({ type, payload }) => { | |
903 | if (type === 'state-change') return this.handleLiveStateChange(payload.state) | |
904 | if (type === 'views-change') return this.handleLiveViewsChange(payload.views) | |
905 | }) | |
906 | } | |
907 | ||
908 | private handleLiveStateChange (newState: VideoState) { | |
909 | if (newState !== VideoState.PUBLISHED) return | |
910 | ||
911 | const videoState = this.video.state.id | |
912 | if (videoState !== VideoState.WAITING_FOR_LIVE && videoState !== VideoState.LIVE_ENDED) return | |
913 | ||
914 | console.log('Loading video after live update.') | |
915 | ||
916 | const videoUUID = this.video.uuid | |
917 | ||
918 | // Reset to refetch the video | |
919 | this.video = undefined | |
920 | this.loadVideo(videoUUID) | |
921 | } | |
922 | ||
923 | private handleLiveViewsChange (newViews: number) { | |
924 | if (!this.video) { | |
925 | console.error('Cannot update video live views because video is no defined.') | |
926 | return | |
927 | } | |
928 | ||
e43b5a3f C |
929 | console.log('Updating live views.') |
930 | ||
a800dbf3 C |
931 | this.video.views = newViews |
932 | } | |
933 | ||
941c5eac C |
934 | private initHotkeys () { |
935 | this.hotkeys = [ | |
941c5eac | 936 | // These hotkeys are managed by the player |
66357162 C |
937 | new Hotkey('f', e => e, undefined, $localize`Enter/exit fullscreen (requires player focus)`), |
938 | new Hotkey('space', e => e, undefined, $localize`Play/Pause the video (requires player focus)`), | |
939 | new Hotkey('m', e => e, undefined, $localize`Mute/unmute the video (requires player focus)`), | |
941c5eac | 940 | |
66357162 | 941 | 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 | 942 | |
66357162 C |
943 | new Hotkey('up', e => e, undefined, $localize`Increase the volume (requires player focus)`), |
944 | new Hotkey('down', e => e, undefined, $localize`Decrease the volume (requires player focus)`), | |
941c5eac | 945 | |
66357162 C |
946 | new Hotkey('right', e => e, undefined, $localize`Seek the video forward (requires player focus)`), |
947 | new Hotkey('left', e => e, undefined, $localize`Seek the video backward (requires player focus)`), | |
941c5eac | 948 | |
66357162 C |
949 | new Hotkey('>', e => e, undefined, $localize`Increase playback rate (requires player focus)`), |
950 | new Hotkey('<', e => e, undefined, $localize`Decrease playback rate (requires player focus)`), | |
941c5eac | 951 | |
66357162 | 952 | new Hotkey('.', e => e, undefined, $localize`Navigate in the video frame by frame (requires player focus)`) |
941c5eac | 953 | ] |
3d216ea0 C |
954 | |
955 | if (this.isUserLoggedIn()) { | |
956 | this.hotkeys = this.hotkeys.concat([ | |
957 | new Hotkey('shift+l', () => { | |
958 | this.setLike() | |
959 | return false | |
66357162 | 960 | }, undefined, $localize`Like the video`), |
3d216ea0 C |
961 | |
962 | new Hotkey('shift+d', () => { | |
963 | this.setDislike() | |
964 | return false | |
66357162 | 965 | }, undefined, $localize`Dislike the video`), |
3d216ea0 C |
966 | |
967 | new Hotkey('shift+s', () => { | |
968 | this.subscribeButton.subscribed ? this.subscribeButton.unsubscribe() : this.subscribeButton.subscribe() | |
969 | return false | |
66357162 | 970 | }, undefined, $localize`Subscribe to the account`) |
3d216ea0 C |
971 | ]) |
972 | } | |
973 | ||
974 | this.hotkeysService.add(this.hotkeys) | |
941c5eac | 975 | } |
dc8bc31b | 976 | } |