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